I am attempting to use the Observer feature in ActiveRecord to monitor a
set of objects. My observer is not named after the objects it is
monitoring (because it is monitoring more than one object) so I have the
following in my environment.rb:
Rails::Initializer.run do |config|
config.active_record.observers = :message_observer
My observer looks something like this:
class MessageObserver < ActiveRecord::Observer
observe Topic, Comment
...code to handle event...
My problem was that the observer was not firing. After extensive
debugging I have determined the problem. If I start up my development
environment (Webrick) I have verified the observation is setup correctly
and my observer is observing the correct models when the first request
is made. When I make a second request the observation is no longer setup
and therefore my observer does not fire. So if in the first request I
carry out the action that triggers the observer everything works fine.
But every request after that fails.
I am guessing the problem has something to do with the automatic
reloading of the models during the development environment. So my
question is how do I fix this?