我有一个模型对象(比方说父)与另一个模型对象(子)上的 has_many 关联。
class Parent < ActiveRecord::Base
has_many :children
accepts_nested_attributes_for :children
end
class Child < ActiveRecord::Base
belongs_to :parent
end
在父级上,我想在 before_update 回调上添加代码以根据其子级设置计算属性。
我遇到的问题是,当我使用 Parent.update(id, atts) 方法为新子代添加 atts 时,atts 集合中添加的那些在 before_update 期间不可用(self.children 返回旧集合) .
有没有办法在不弄乱accept_nested_attributes_for的情况下检索新的?