希望有人可以帮助我理解为什么会发生这种情况。我设置了以下情况...外键删除新/建
@product = Product.find(params[:id])
@new_image = @product.images.new
当调试@new_image
,它具有正确的外键设置。
--- !ruby/object:Image
attributes:
product_id: 1
但是,保存时,没有设置product_id
。那是当我注意到,也在那个调试信息,这是...
changed_attributes:
product_id:
基本上无效我的外键。同样的事情,如果我使用构建。为什么这个实例不能保持外键?
UPDATE: 使事情变得更简单,即使我只是在我看来输出debug Product.find(1).images.new
,我得到:
!ruby/object:ProductImage
attributes:
created_at:
product_id: 1
updated_at:
attributes_cache: {}
changed_attributes:
product_id:
destroyed: false
marked_for_destruction: false
new_record: true
previously_changed: {}
readonly: false
您可以发布您全面控制代码,不会保存,也许你的`Product`和`Image`模型的内容? – 2011-01-29 03:05:28