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.
article.rb:class Article
include Mongoid::Document
field :name
field :body
field :slug
before_create :generate_slug
protected
def generate_slug
self.slug = name.to_permalink
end
end
Callbacks are coming from ActiveModel, so you can use the new syntax as well:
article.rb:class Article
include Mongoid::Document
field :name
set_callback(:save, :before) do |document|
document.generate_slug
end
protected
def generate_slug
self.slug = name.to_permalink
end
end