Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7089313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:54:24+00:00 2026-05-28T07:54:24+00:00

Is there a way to dynamically add after_add and after_remove callbacks to an existing

  • 0

Is there a way to dynamically add after_add and after_remove callbacks to an existing has_many or has_and_belongs_to_many relationship?

For example, suppose I have models User, Thing, and a join model UserThingRelationship, and the User model is something like this:

class User < ActiveRecord::Base
  has_many :user_thing_relationships
  has_many :things, :through => :user_thing_relationships
end

I’d like to be able to, in a module that extends User, add :after_add and :after_remove callbacks to the User.has_many(:things, ...) relationship. I.e., have something like

module DoesAwesomeStuff
  def does_awesome_stuff relationship, callback
    # or however this can be achieved...
    after_add(relationship) callback
    after_remove(relationship) callback
  end
end

So that

class User < ActiveRecord::Base
  has_many :user_thing_relationships
  has_many :things, :through => :user_thing_relationships

  does_awesome_stuff :things, :my_callback
  def my_callback; puts "awesome"; end
end

Is effectively the same as

class User < ActiveRecord::Base
  has_many :user_thing_relationships
  has_many :things, :through => :user_thing_relationships, :after_add => :my_callback, :after_remove => :my_callback

  def my_callback; puts "awesome"; end
end

This can be done pretty effectively for adding after_save, etc, callbacks to the model that’s being extended, since ActiveRecord::Base#after_save is just a class method.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T07:54:24+00:00Added an answer on May 28, 2026 at 7:54 am

    I was able to come up with the following by using ActiveRecord::Reflection:

    module AfterAdd
      def after_add rel, callback
        a = reflect_on_association(rel)
        send(a.macro, rel, a.options.merge(:after_add => callback))
      end
    end
    
    class User < ActiveRecord::Base
      extend AfterAdd
    
      has_many :user_thing_relationships
      has_many :things, :through => :user_thing_relationships
    
      after_add :things, :my_callback
    
      def my_callback
        puts "Hello"
      end
    end
    

    I don’t want to answer my own question, so I won’t give myself answer credit if someone else can come up with a better solution in the next few days.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way to dynamically generate an swf file? Or insert variables after
Is there a way to dynamically set the ListId field in the ListView control.
Is there a way to dynamically create a selectItem list? I dont really want
Is there a way to dynamically and conditionally create a class definition in PHP,
Is there a way to dynamically create a kind of sub-domain that goes between
Is there an easy way of dynamically building a filepath in .Net? At the
Is there a way to automatically/dynamically set all the attributes for a datamapper object
Is there a way in C to find out the size of dynamically allocated
is there any way to set the friction of a physics body dynamically in
The table names in my mysql database are dynamically generated. Is there some way

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.