I am interested in any solution the title describes.

My gemfile is:

gem "rails", "~> 4.0.0"

gem "mongoid", "~> 3.1.3"
gem 'rails-observers'

I want to use observers on my mongoid models but I receive this error:

https://github.com/mongoid/mongoid/issues/3108

Any ideas are welcome

有帮助吗?

解决方案

I just implemented this gem, https://github.com/chamnap/mongoid-observers/, because I often need it. Please have a look and give me feedback.

其他提示

It looks like you need to include the module in each model and set the observers.

class ORM
  include ActiveModel::Observing
end

# Calls PersonObserver.instance
ORM.observers = :person_observer

# Calls Cacher.instance and GarbageCollector.instance
ORM.observers = :cacher, :garbage_collector

# Same as above, just using explicit class references
ORM.observers = Cacher, GarbageCollector

https://github.com/rails/rails-observers/blob/master/lib/rails/observers/active_model/observing.rb#L19

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top