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

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 =>
:after_add => :after_add_child, :before_remove =>
belongs_to :parent, :class_name => ‘Company’, :foreign_key =>

something similar to :after_add above?



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