rails-observers icon indicating copy to clipboard operation
rails-observers copied to clipboard

undefined method add_observer when using Observer for Mailer from Rails Engine

Open AndyObtiva opened this issue 8 years ago • 1 comments

I keep getting this error when I start a Rails 4.2.4 application consuming a Rails Engine with a Mailer Observer configured.

Details are below with some code information replaced with placeholders to protect my client's code rights.

engine.rb configuration:

config.autoload_paths += %W(#{config.root}/app/observers)
config.active_record.observers = 'NameOfModule::NameOfObserver'

observer code:

module NameOfModule
  class NameOfObserver < ActiveRecord::Observer
    observe 'NameOfModule::NameOfMailer'
    ...

Error:

$ rails s
=> Booting Thin
=> Rails 4.2.4 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
Exiting
/Users/User/.rvm/gems/ruby-2.3.0@code/gems/attr_encrypted-1.3.5/lib/attr_encrypted.rb:259:in `method_missing': undefined method `add_observer' for Buccaneer::EntryMailer:Class (NoMethodError)
    from /Users/User/.rvm/gems/ruby-2.3.0@code/gems/actionmailer-4.2.4/lib/action_mailer/base.rb:569:in `method_missing'
    from /Users/User/.rvm/gems/ruby-2.3.0@code/gems/rails-observers-0.1.2/lib/rails/observers/active_model/observing.rb:364:in `add_observer!'
    from /Users/User/.rvm/gems/ruby-2.3.0@code/gems/rails-observers-0.1.2/lib/rails/observers/activerecord/observer.rb:105:in `add_observer!'
    from /Users/User/.rvm/gems/ruby-2.3.0@code/gems/rails-observers-0.1.2/lib/rails/observers/active_model/observing.rb:341:in `block in initialize'
    from /Users/User/.rvm/gems/ruby-2.3.0@code/gems/rails-observers-0.1.2/lib/rails/observers/active_model/observing.rb:341:in `each'
    from /Users/User/.rvm/gems/ruby-2.3.0@code/gems/rails-observers-0.1.2/lib/rails/observers/active_model/observing.rb:341:in `initialize'

I will add any further details if I discover any.

AndyObtiva avatar May 11 '16 17:05 AndyObtiva

OK, I fixed by not setting config.active_record.observers= in engine.rb nor having an observe class method call in Observer, yet instead just using register_observer class method call in Mailer itself. Must be a special issue with Rails Mailer Observers.

Still, it would be nice not to get that exception for add_observer (is that old API?) when I register via observe class method in Observer instead of using register_observer

AndyObtiva avatar May 11 '16 18:05 AndyObtiva