This project is archived and is in readonly mode.

#1532 ✓invalid
Avi Flombaum

add_observer doesn't add to observers

Reported by Avi Flombaum | December 8th, 2008 @ 07:15 PM | in 2.x

So AR has a model.add_observer method. It's undocumented (I can't even find it in the source other then the fact that it's called in the add_observer! definition in activerecord/observers.rb:

def add_observer!(klass)
  if respond_to?(:after_find) && !klass.method_defined?(:after_find)
    klass.class_eval 'def after_find() end'

So I'm calling add_observer in a before_filter in controllers and I want to make sure that the observer hasn't already been added to the model. I call model.observers hoping to receive an array of the observers that have been added and it returns empty, even though add_observer has fired and the model is being observed. If I do model.observers << my_observer_instance then the observer_instance is added to model.observers but no observing actually occurs. So I need to do both model.add_observer(my_observer_instance) and model.observers << my_observer_instance.

Shouldn't add_observer (wherever it is defined), also add that observer to the observers instance var? Sorry if this was a convoluted way of expressing the bug.

Comments and changes to this ticket

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>

People watching this ticket