Stijnster
2010-Jul-15 16:46 UTC
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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
Mislav Marohnić
2010-Jul-16 10:59 UTC
Re: Observers are not loading in development mode (Rails 3 beta 4)
On Thu, Jul 15, 2010 at 18:46, Stijnster <s.mathysen@telenet.be> wrote:> > 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) -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
Stijnster
2010-Jul-16 12:47 UTC
Re: Observers are not loading in development mode (Rails 3 beta 4)
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 On Jul 16, 12:59 pm, Mislav Marohnić <mislav.maroh...@gmail.com> wrote:> On Thu, Jul 15, 2010 at 18:46, Stijnster <s.mathy...@telenet.be> wrote: > > > 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)-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
andrea
2010-Jul-23 11:35 UTC
Re: Observers are not loading in development mode (Rails 3 beta 4)
On 16 Lug, 14:47, Stijnster <s.mathy...@telenet.be> wrote:> 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.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. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.