Pages

Thursday, October 17, 2013

Observers in Rails 3

Rails observers are sweet, You can observe multiple models within a single observer
First, you need to generate your observer:

rails g observer Auditor

Then, in your fresh auditor_observer.rb file define the models you wish to observe and then add theafter_create callback.

class AuditorObserver < ActiveRecord::Observer
   observe :model_foo, :model_bar, :model_baz

   def after_create(record)
    #do something with `record`
   end
end



No comments:

Post a Comment