2010-06-22 62 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回調,並在那裏顯式「觸摸」公司實例。