Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Observers on a model are called multiple times

#764

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)

observerd_model.save

end

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.

Reported by Gudata · August 6th, 2008 @ 03:28 PM

State: wontfix
Milestone: 2.x
Assigned to: Michael Koziarski Michael Koziarski
Importance: none

Activity

  1. Andrei
    Andrei

    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

    November 4th, 2008 @ 11:21 PM

  2. Han-Shen Yuan
    Han-Shen Yuan

    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.

    November 21st, 2008 @ 01:00 AM

  3. Frederik Fix
    Frederik Fix
    • 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
    
    

    end end

    Also see attached patch for rails-core.

    December 14th, 2008 @ 07:04 PM

  4. Frederik Fix
  5. Michael Koziarski
    Michael Koziarski
    • 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?

    December 17th, 2008 @ 10:27 AM

  6. Frederik Fix
    Frederik Fix

    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).

    December 17th, 2008 @ 12:47 PM

  7. Michael Koziarski
    Michael Koziarski

    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.

    December 17th, 2008 @ 12:50 PM

  8. Frederik Fix
    Frederik Fix

    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.

    December 17th, 2008 @ 01:41 PM

  9. christos.pappas (at gmail)
    christos.pappas (at gmail)

    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.

    August 24th, 2009 @ 11:54 AM

  10. bingbing