2010-06-22 61 views
3

保存父对象时如何在关联上调用“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”,但不在公司上调用“before_save”。谢谢。

回答

2

您可以使用此patch,它将“touch”功能添加到has_one关联,或者只是在用户模型中定义另一个after_save回调,并在那里显式“触摸”公司实例。