1

我正在尝试为我的项目生成NotificationMessageTask我的项目中生成。我正在使用Mongoid 2.

我创建了一个NotificationObserver,在这个观察者中是否有可能Message只观察和after_create观察Task两者。就像这里描述的那样:http: //jamesgolick.com/2009/8/5/observational-better-observers-for-activerecord.htmlafter_createafter_update

已经很长时间了,所以我认为它现在可能是开箱即用的,但我在文档中找不到它。

4

1 回答 1

0

对易于处理的不同类使用两个不同的观察者,您可以相应地捕获回调

或试试这个

class NotificationObserver < ActiveRecord::Observer
   observe :task, :message

   def after_create(record)
      if record.class == Task
         do task stuff
      else
         do message stuff
      end
   end

   def after_update(record)
      if record.class == Task
         do task stuff
      end
   end
end
于 2012-09-05T06:03:14.247 回答