Topic: SOLVED - Observer generates uninitialized constant

Hi, I'm trying to use an observer (since Rails creates it in models folder I post my issue in this section) but after creating it my server doesn't start anymore.

I created in 'models' folder a file called mailer_observer.rb

class MailerObserver < ActiveRecord::Observer
end

I added into config/environment.rb
  # Activate observers that should always be running
   config.active_record.observers = :mailer_observer

after that I tried to reboot my server but this is what happened
Library/Ruby/Gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:266:in `load_missing_constant': uninitialized constant Mailer (NameError)
    from /Library/Ruby/Gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:453:in `const_missing'
    from /Library/Ruby/Gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:465:in `const_missing'
    from /Library/Ruby/Gems/1.8/gems/activesupport-2.0.2/lib/active_support/inflector.rb:257:in `constantize'
    from /Library/Ruby/Gems/1.8/gems/activesupport-2.0.2/lib/active_support/core_ext/string/inflections.rb:148:in `constantize'
    from /Library/Ruby/Gems/1.8/gems/activerecord-2.0.2/lib/active_record/observer.rb:143:in `observed_class'
    from /Library/Ruby/Gems/1.8/gems/activerecord-2.0.2/lib/active_record/observer.rb:169:in `observed_classes'
    from /Library/Ruby/Gems/1.8/gems/activerecord-2.0.2/lib/active_record/observer.rb:152:in `initialize'
    from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/singleton.rb:95:in `new'
     ... 35 levels...
    from /Library/Ruby/Gems/1.8/gems/rails-2.0.2/lib/commands/server.rb:39
    from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require'
    from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rubygems/custom_require.rb:27:in `require'
    from script/server:3

What am I missing ???

Thanks and have a nice day!

Last edited by macsig (2008-03-20 02:25:10)

Re: SOLVED - Observer generates uninitialized constant

My guess, and it is just a guess since I've haven't really used observers, is that it is looking for a class called Mailer. I'm assuming the standard naming convention for an observer is xxxxMailer where xxxx is the model you want to observe.
So, if you were to observe a Project, you would have a ProjectObserver. If you were to observer a User you would have a UserObserver.
These might listen for certain methods, which than call a Mailer Class (which it seems you're trying to do here).

(Like I said, I haven't used Observers before, so its all conjecture)

Re: SOLVED - Observer generates uninitialized constant

Thanks to your reply I solved it: since I want to have a single observer for the whole application that basically works with a mailer, I called it MailerObserver even if I don't have any model called mailer. Only with

class MailerObserver < ActiveRecord::Observer
end

as you said Rails was looking for a mailer class (since by convention the observer has the model name)
Adding

 class MailerObserver < ActiveRecord::Observer
   observe :user
end

Rails now looks for user model and everything works fine.

HAVE A NICE DAY!