This project is archived and is in readonly mode.

#764 ✓wontfix

Observers on a model are called multiple times

Reported by Gudata | August 6th, 2008 @ 03:28 PM | in 2.x

rails 2.1 in development mode only

I have an observer for a model.

I have after_save in the observer class.

In test controller i have action:

def test_save

observerd_model = ObservedModel.find(:first)


The after_save is called once.

If I retry the same action after_save is called twice. and so on...

When I dump the object_id from the observer it seems that the old observers are kept and new one is created each time.

Comments and changes to this ticket

  • Andrei

    Andrei November 4th, 2008 @ 11:21 PM

    This happened to me as well when I was trying to observe all models (not a good idea):

    def observed_class ActiveRecord::Base end

    So returning an array of observed classes fixed this problem for me:

    def observed_class [ User, Article, Photo, Comment, Rating ] end

  • Han-Shen Yuan

    Han-Shen Yuan November 21st, 2008 @ 01:00 AM

    I've seen this problem as well on Rails 2.1.2. In my case, the after_create method was being called multiple times. This is a serious issue and makes using observers basically unusable while in development mode (or take the hit and develop in production mode). Presumably this is related to caching.

  • Frederik Fix

    Frederik Fix December 14th, 2008 @ 07:04 PM

    • Tag set to patch

    I've run into this problem as well on Rails 2.2.2

    After some digging I have found out that the problem is caused by the class files being required multiple times, which then causes the callbacks to be defined again and again.

    You can paste the following into the beginning of config/environment.rb:

    module ActiveSupport module Callbacks

    class CallbackChain < Array
      def concat(chain)
        present = map(&:method)
        super chain.reject { |c| present.include?(c.method) }

    end end

    Also see attached patch for rails-core.

  • Frederik Fix
  • Michael Koziarski

    Michael Koziarski December 17th, 2008 @ 10:27 AM

    • Assigned user set to “Michael Koziarski”
    • State changed from “new” to “wontfix”

    Requiring the class files multiple times isn't something that we support, as you'll probably hit other issues too.

    Similarly this only fixes it for callbacks which use methods, it doesn't (and can't) handle cases where the callback is a block right?

  • Frederik Fix

    Frederik Fix December 17th, 2008 @ 12:47 PM

    That's correct. Just tried it and indeed if the callback uses a block this won't work.

    It's probably better to fix the underlying issue of requiring the class file multiple times (in the app).

  • Michael Koziarski

    Michael Koziarski December 17th, 2008 @ 12:50 PM

    Did you have any require statements in your application?

    Essentially our code shouldn't ever do that, if you've triggered it somehow we should open another ticket.

  • Frederik Fix

    Frederik Fix December 17th, 2008 @ 01:41 PM

    Yes I had additional require statements in my code. Once I removed those the problem went away.

    Your judgement to not fix this is correct in my opinion.

  • christos.pappas (at gmail)

    christos.pappas (at gmail) August 24th, 2009 @ 11:54 AM

    I'm getting this same issue however the model i'm observing is in a plugin... If I move the model into my app folder the issue goes away. I'm assuming it has something to do with the way plugins get loaded? The plugin i'm getting this with is acts_as_follower. I've removed the follow model require in init.rb and it still happens.

  • bingbing

    bingbing March 26th, 2011 @ 02:09 AM

    • Importance changed from “” to “”

Create your profile

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

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href=""></a>