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 end My observer looks something like this: class MessageObserver < ActiveRecord::Observer observe Topic, Comment def after_create(message) ...code to handle event... end end 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? Eric --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Eric Anderson
2006-Sep-09 20:03 UTC
Re: Observer: Loosing observation after initial request
Eric Anderson wrote:> 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?The workaround I found based on a blog post[1] was to put this in the ApplicationController: observer :message_observer This way the observer is setup on every request since ApplicationController is reloaded. But this seems like the wrong strategy and like the blog post indicated this will not work in a script or console environment if the environment is reloaded. Anybody know of a better way? 1. http://www.robbyonrails.com/articles/2006/02/27/where-did-my-observer-go --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---