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.

add_observer doesn't add to observers

#1532

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)
  klass.add_observer(self)
  if respond_to?(:after_find) && !klass.method_defined?(:after_find)
    klass.class_eval 'def after_find() end'
  end
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.

Reported by Avi Flombaum · December 8th, 2008 @ 07:15 PM

State: invalid
Milestone: 2.x
Assigned to: nobody
Importance: none

Activity

  1. Robby Russell
    Robby Russell

    Avi,

    Observers are declared in your environment.rb file now. You shouldn't be using observers via controllers anymore.

    February 24th, 2009 @ 05:03 AM

  2. Pratik
    Pratik
    • State changed from new to invalid

    What Robby said.

    August 5th, 2009 @ 02:43 PM