Mongoid supports the following callbacks:
Callbacks are available on any document, whether it is embedded within another document or not. Note that to be efficient, Mongoid only fires the callback of the document that the persistence action was executed on. This is that Mongoid aims to support large hierarchies and to handle optimized atomic updates callbacks can't be firing all over the document hierarchy.
Using callbacks for domain logic is a bad design practice, and can lead to unexpected errors that are hard to debug when callbacks in the chain halt execution. It is our recommendation to only use them for cross-cutting concerns, like queueing up background jobs.
class Article include Mongoid::Document field :name, type: String field :body, type: String field :slug, type: String before_create :send_message after_save do |document| # Handle callback here. end protected def send_message # Message sending code here. end end
Callbacks are coming from Active Support, so you can use the new syntax as well:
class Article include Mongoid::Document field :name, type: String set_callback(:create, :before) do |document| # Message sending code here. end end
Relation CallbacksSince 3.1.0
Mongoid has a set of callbacks that are specific to collection based relations - these are:
Observer classes respond to life cycle callbacks to implement trigger-like behavior outside the original class. This is a great way to reduce the clutter that normally comes when the model class is burdened with functionality that doesn't pertain to the core responsibility of the class. Mongoid's observers work similar to ActiveRecord's. Example:
class ArticleObserver < Mongoid::Observer def after_save(article) Notifications.article("email@example.com", "New article", article).deliver end end
Observers are available for any document, whether it is embedded within another document or not. Note that to be efficient, Mongoid only fires the observers of the document that the persistence action was executed on. This is that Mongoid aims to support large hierarchies and to handle optimized atomic updates callbacks can't be firing all over the document hierarchy.
Observers will not be invoked unless they are instantiated first. If you are
using Rails, Mongoid will instantiate your observers automatically as long as
you register them in your
config/application.rb file like so:
config.mongoid.observers = :article_observer, :audit_observer
If you're not using Rails, then you will have to load and register your observers directly with Mongoid and afterwards instruct Mongoid to instantiate them before they will work. Instantiating an observer registers it with its observed model(s) so they will need to be loaded beforehand.
require "article_observer" require "audit_observer" Mongoid.observers = ArticleObserver, AuditObserver Mongoid.instantiate_observers
Observers will by default be mapped to the class with which they share a name. So CommentObserver will be tied to observing Comment, ProductManagerObserver to ProductManager, and so on. If you want to name your observer differently than the class you're interested in observing, you can use the Observer.observe class method which takes either the concrete class (Product) or a symbol for that class (:product). If an observer needs to watch more than one kind of object, this can be specified with multiple arguments.
class AuditObserver < Mongoid::Observer observe :account, :balance def after_update(record) AuditTrail.new(record, "UPDATED") end end