#575 √ wontfix
Christopher J. Bottaro

callbacks don't work from extended modules

Reported by Christopher J. Bottaro | July 8th, 2008 @ 12:28 PM | in 2.x

If an AR instance extends a module that adds callbacks to the instance's metaclass, then those callbacks do not get called when the callback chain is invoked.

I have included a patch that fixes the problem as well as adds a test case. All tests pass in activesupport after the patch.

Please see the test case included in the patch for a very clear description of the problem.

This patch is meant to be applied from SRC_ROOT/activesupport with -p0.

This patch is for Rails 2.1.0.

Comments and changes to this ticket

  • Christopher J. Bottaro

    Christopher J. Bottaro July 8th, 2008 @ 12:29 PM

    I forgot to mention, this patch is for Rails 2.1.0.

  • Pratik

    Pratik July 14th, 2008 @ 08:49 AM

      • → Assigned user changed from “” to “Joshua Peek”
  • Repository

    Repository July 15th, 2008 @ 09:58 PM

      • → State changed from “new” to “resolved”

    (from [be078ee162fcae883a5621a30929879cd783a238]) Run callbacks from object's metaclass [#575 state:resolved]

    http://github.com/rails/rails/co...

  • Joshua Peek

    Joshua Peek July 16th, 2008 @ 09:26 PM

      • → State changed from “resolved” to “open”

    We had to revert this. Apparently it broke a ton of shit.

    Maybe we don't want callbacks on the metaclass, but a set of callbacks for objects as well as classes.

    Lets try a new API

    module ModuleWithCallbacks
      def self.extended(obj)
        obj.before_save :module_callback
      end
    
      def module_callback
        # ...
      end
    end
    
    p = Person.new
    p.extend(ModuleWithCallbacks)
    
    # or also do
    p.before_save :module_callback
    def p.module_callback
      ...
    end
    

    We could have a instance variable on the object that tracks its callbacks

  • Christopher J. Bottaro

    Christopher J. Bottaro July 16th, 2008 @ 09:55 PM

    Just out of curiosity, what did it break?

    It broke our webapp because we try to serialize AR instances and it complained about not being able to serialize the object's metaclass. We just decided that we shouldn't have been serializing AR instances in the first place, heh.

  • Christopher J. Bottaro

    Christopher J. Bottaro July 16th, 2008 @ 09:59 PM

    Btw, your solution of having callback chains on classes as well as instances of classes (objects) sounds great.

  • Joshua Peek

    Joshua Peek July 18th, 2008 @ 07:30 PM

    Check out the latest Memoizable stuff for reference.

    http://github.com/rails/rails/tr...

    I've been playing around with a pattern that works for Classes and Objects.

    I don't have time to work on this right now, but I do plan to get back to in in the next coming weeks.

    (BTW, I'm getting obsessed with "object-oriented"/prototype driven development)

  • Joshua Peek

    Joshua Peek July 22nd, 2008 @ 10:30 AM

      • → State changed from “open” to “wontfix”

    It looks like this is going take alot more work. If you ever decide its worth pursuing, open and new ticket for refactoring Callbacks and assign it to me.

Please Login or create a free account to add a new comment.

You can update this ticket by sending an email to from your email client. (help)

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

Source available from github

The Git repository resides at http://github.com/rails

Check out the current development trunk (Edge Rails) with:

git clone git://github.com/rails/rails.git

The latest development for the 1.2.x and 2.0.x releases are on the 1-2-stable and 2-0-stable branches.

Creating a bug report

When creating a bug report, be sure to include as much relevant information as possible. Post the code sample that causes the problem. Preferably, alter the unit tests and show through either changed or added tests how the expected behavior is not occuring.

Security vulnerabilities should be reported via an email to security@rubyonrails.org, do not use trac for reporting security vulnerabilities. All content in trac is publicly available as soon as it is posted.

Then don't get your hopes up. Unless you have a "Code Red, Mission Critical, The World is Coming to an End" kinda bug, you're creating this ticket in the hope that others with the same problem will be able to collaborate with you on solving it. Do not expect that the ticket automatically will see any activity or that others will jump to fix it. Creating a ticket like this is mostly to help yourself start on the path of fixing the problem and for others to sign on to with a "I'm having this problem too".

Shared Ticket Bins

Attachments