This project is archived and is in readonly mode.
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)
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.
Comments and changes to this ticket
-
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 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 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
end end
Also see attached patch for rails-core.
-
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 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 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 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) 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.
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="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>