title: Dynamically create after_add and after_remove callbacks for has_many or habtm?
body: <p>Is there a way to dynamically add <code>after_add</code> and <code>after_remove</code> callbacks to an existing <code>has_many</code> or <code>has_and_belongs_to_many</code> relationship?</p>
  
  <p>For example, suppose I have models <code>User</code>, <code>Thing</code>, and a join model <code>UserThingRelationship</code>, and the <code>User</code> model is something like this:</p>
  
  <pre><code>class User &lt; ActiveRecord::Base
    has_many :user_thing_relationships
    has_many :things, :through =&gt; :user_thing_relationships
  end
  </code></pre>
  
  <p>I'd like to be able to, in a module that extends <code>User</code>, add <code>:after_add</code> and <code>:after_remove</code> callbacks to the <code>User.has_many(:things, ...)</code> relationship.  I.e., have something like</p>
  
  <pre><code>module DoesAwesomeStuff
    def does_awesome_stuff relationship, callback
      # or however this can be achieved...
      after_add(relationship) callback
      after_remove(relationship) callback
    end
  end
  </code></pre>
  
  <p>So that</p>
  
  <pre><code>class User &lt; ActiveRecord::Base
    has_many :user_thing_relationships
    has_many :things, :through =&gt; :user_thing_relationships
  
    does_awesome_stuff :things, :my_callback
    def my_callback; puts "awesome"; end
  end
  </code></pre>
  
  <p>Is effectively the same as</p>
  
  <pre><code>class User &lt; ActiveRecord::Base
    has_many :user_thing_relationships
    has_many :things, :through =&gt; :user_thing_relationships, :after_add =&gt; :my_callback, :after_remove =&gt; :my_callback
  
    def my_callback; puts "awesome"; end
  end
  </code></pre>
  
  <p>This can be done pretty effectively for adding <code>after_save</code>, etc, callbacks to the model that's being extended, since <code>ActiveRecord::Base#after_save</code> is just a class method.</p>