Observers are not loading in development mode (Rails 3 beta 4)

Hi all,

I got a strange problem. I'm using rspec to develop some new features
on a rails 3 app.

I load my observers from the application.rb file like this;

    config.active_record.observers = :admin_observer,
'Message::MailObserver', 'Message::EventObserver'

When writing tests and doing BDD the observers are loaded correct.

Then when I go into development mode the observers aren't loaded or
executed.

When I change my development setting to;

  config.cache_classes = true

the observers get loaded and executed.

Does anyone else have the same problems?

I'm currently on Rails 3 beta 4 running ruby-1.8.7-p299

Hope you can help me out!

Stijn

I load my observers from the application.rb file like this;

config.active_record.observers = :admin_observer,

‘Message::MailObserver’, ‘Message::EventObserver’

That’s correct. This syntax hasn’t changed.

When I change my development setting to;

config.cache_classes = true

the observers get loaded and executed.

This would indicate that, normally in development mode, observer classes don’t get instantiated at all. This is strange, and I can’t reproduce this failure. I’ve just checked activerecord Railtie and the hooks to instantiate observers; it seems like everything is in place. Observer classes should always be instantiated once when ActiveRecord::Base is loaded and before every server request in development mode.

Try to provide us with more info. First, reset cache_classes setting to false (development mode default). Next, use the rails console to check up on the following:

  1. ActiveRecord::Base.observers — it should match what you specified in config.active_record.observers
  2. MyModel.count_observers — replace “MyModel” with the name of the model having observers. It will return the number of observers tied to this model; otherwise it will fail with a NoMethodError because the array hasn’t been initialized (this is a minor bug that I’m patching right now)

Hi Mislav,

I've reset the value of config.cache_classes back to false and ran
both commands in the console;

ActiveRecord::Base.observers
=> ["Message::MailObserver", "Message::EventObserver"]

That's ok (I removed the AdminObserver from the previous example since
it was only a test)

Message::Mail.count_observers

NoMethodError: You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.size

That gives the error you describe.

Hope this helps. Strange that you don't have this behaviour in your
app though...

Thanks for following up!

Stijn

The same is happening now to me with same env(Rails 3 beta4 and Ruby
1.8.7. Just to point out it's not a single case.