has_one belongs_to association autosave => true不保存
我有两个型号
Board has_one :pref, :autosave => true, :dependent => :destroy Pref belongs_to :board
pref对象具有在数据库中设置的默认值,因此在创建板时不需要使用信息来创建对象。 电路板的ID在pref表中。
由于:autosave => true,我认为当我创建并保存新的Board对象时,将自动创建并保存pref对象。
这不是这样的,所以我一定是误会。
有没有办法在保存电路板时自动保存pref对象?
先感谢您
autosave => true
不应该为你创建一个元素。 文档说 :
如果为true,则在保存父对象时,始终保存关联对象或在标记为销毁时将其销毁。 如果为false,则永远不要保存或销毁关联对象。
您可以在创建新board
时使用回调来创建pref
对象。
有点像:
after_create :create_pref def create_pref pref.create! end