【发布时间】:2010-01-07 14:49:43
【问题描述】:
我有一个模型对象(假设是 Parent),它与另一个模型对象(Child)上的 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 时,在 before_update 期间添加到 atts 集合中的那些不可用(self.children 返回旧收藏)。
有没有办法在不弄乱accept_nested_attributes_for 的情况下检索新的?
【问题讨论】:
标签: ruby-on-rails callback nested-attributes