Rails更新属性始终插入nil

时间:2015-07-15 20:32:50

标签: ruby-on-rails rails-console

数据库更新出现问题。起初我以为它可能与Carrierwave有关,但经过这次测试我不再这么认为了。

这里我尝试更新rails控制台中的属性,只是让图像字段返回nil。但请注意,property_id字段会更改。

2.2.0 :004 > Propimage
 => Propimage(id: integer, property_id: integer, image: string, created_at: datetime, updated_at: datetime) 
2.2.0 :005 > Propimage.last
  Propimage Load (0.7ms)  SELECT  "propimages".* FROM "propimages"  ORDER BY "propimages"."id" DESC LIMIT 1
 => #<Propimage id: 37, property_id: 5, image: nil, created_at: "2015-07-15 20:06:55", updated_at: "2015-07-15 20:22:30"> 
2.2.0 :006 > Propimage.last.update_attributes(image: 'Picture.jpg', property_id: 3)
  Propimage Load (0.3ms)  SELECT  "propimages".* FROM "propimages"  ORDER BY "propimages"."id" DESC LIMIT 1
   (0.1ms)  begin transaction
  Propimage Load (0.2ms)  SELECT  "propimages".* FROM "propimages" WHERE "propimages"."id" = ? LIMIT 1  [["id", 37]]
  SQL (0.5ms)  UPDATE "propimages" SET "image" = ?, "property_id" = ?, "updated_at" = ? WHERE "propimages"."id" = ?  [["image", nil], ["property_id", 3], ["updated_at", "2015-07-15 20:24:06.501042"], ["id", 37]]
   (1.1ms)  commit transaction
 => true 
2.2.0 :007 > Propimage.last
  Propimage Load (0.2ms)  SELECT  "propimages".* FROM "propimages"  ORDER BY "propimages"."id" DESC LIMIT 1
 => #<Propimage id: 37, property_id: 3, image: nil, created_at: "2015-07-15 20:06:55", updated_at: "2015-07-15 20:24:06"> 
2.2.0 :008 > 

在这里,我尝试创建一个新条目

2.2.0 :009 > Propimage.new(image: 'Picture.jpg', property_id: 5).save
   (0.2ms)  begin transaction
  SQL (1.0ms)  INSERT INTO "propimages" ("image", "property_id", "created_at", "updated_at") VALUES (?, ?, ?, ?)  [["image", nil], ["property_id", 5], ["created_at", "2015-07-15 20:30:17.797735"], ["updated_at", "2015-07-15 20:30:17.797735"]]
   (1.7ms)  commit transaction
 => true 

我的下一步是在数据库中重新创建此表。非常感谢任何帮助。

class CreatePropimages < ActiveRecord::Migration
  def change
    create_table :propimages do |t|
      t.references :property, index: true, foreign_key: true
      t.string :image

      t.timestamps null: false
    end
  end
end

propimage.rb

class Propimage < ActiveRecord::Base
    belongs_to :property
    mount_uploader :image, ImageUploader
end

0 个答案:

没有答案