保存父对象时,如何在关联上调用“before_save”回调?例如:
class Company < ActiveRecord::Base
belongs_to :user
before_save Proc.new { ... } # Not called.
end
class User < ActiveRecord::Base
has_one :company
before_save Proc.new { ... } # Gets called.
end
params = {
:user => {
:name => "Kevin Sylvestre",
:company_attributes => { :city => "Waterloo", :region => "Ontario" }
}
}
@user = User.new(params[:user])
@user.save
对用户调用“before_save”,而不是对公司调用。谢谢。
最佳答案
您可以使用此 patch将“touch”功能添加到 has_one 关联,或者只是在 User 模型中定义另一个 after_save 回调并在那里显式“touch” Company 实例。
关于ruby-on-rails - "Before Save"关联回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3090900/