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」。謝謝。