Using an observer within an Engine

Posted by Tim on Stack Overflow See other posts from Stack Overflow or by Tim
Published on 2012-04-06T11:21:54Z Indexed on 2012/04/06 11:29 UTC
Read the original article Hit count: 183

I've created an Engine which is basically used for all of our projects.

Now what I want to do is add a before_create callback to all of the models in this Engine.

After some searching I found out that an observer is the way to go.

So, I've created this observer:

class AuthObserver < ActiveRecord::Observer

  def before_create( record )
    p record
  end

end

And now I need to add it to the application, but of course in my Engine there is no such file as application.rb.

What I tried is adding it to an initializer located in /config/initializers/observers.rb

Like so:

Rails.application.config.active_record.observers = :auth_observer

But this doesn't work, and it throws no errors.

Anybody out here has experience using an observer inside an engine?

Thanks a lot!

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about observer-pattern