This project is archived and is in readonly mode.
Observers on a model are called multiple times
-
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
-
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.
-
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 endend end
Also see attached patch for rails-core.
-
Frederik Fix
slightly better patch
-
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?
-
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).
-
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.
-
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.
-
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.
