Rails 3 Observer — looking to learn how to implement an Observer for multiple models

痴心易碎 提交于 2019-12-02 17:32:57
jpemberthy

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 the after_create callback.

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

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

In application.rb add

config.active_record.observers = :auditor_observer

And It should work.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!