Adding action after a belongs_to assignment

What is the best way to add action after a belongs_to assignment? (Just
like ‘after_add’ in has_many assignment).

I see that there is no callbacks for belongs_to.

I have tried overriding parent= :

alias :parent_before_mod= :parent=

def parent=(the_parent)
parent_before_mod = the_parent
if save
# my actions here
end
end

but when I tried to assign parent, parent_id was not changed.

Can someone help?

class Company < ActiveRecord::Base
has_many :children, :class_name => ‘Company’, :foreign_key =>
‘parent_id’,
:after_add => :after_add_child, :before_remove =>
:before_remove_child
belongs_to :parent, :class_name => ‘Company’, :foreign_key =>
‘parent_id’

something similar to :after_add above?

end


Endy

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs