开发者

"Before Save" callback on associations

开发者 https://www.devze.com 2023-01-04 12:38 出处:网络
How do you call the \"before_save\" callbacks on an association when saving the parent object? For example:

How do you call the "before_save" callbacks on an association when saving the parent object? For example:

class Company < ActiveRecord::Bas开发者_Go百科e
  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

Does calls "before_save" on the user, but not on the company. Thanks.


You can either use this patch that adds the "touch" functionality to the has_one association or just define another after_save callback in the User model and "touch" the Company instance explicitly there.

0

精彩评论

暂无评论...
验证码 换一张
取 消