It took me a while to find these, so I thought I would post them here. In the end I had to search the source code to find them.
:before_save, :after_save, :before_create, :after_create, :before_update, :after_update, :before_validation, :after_validation, :before_validation_on_create, :after_validation_on_create, :before_validation_on_update, :after_validation_on_update, :before_destroy, :after_destroy, :validate_on_create, :validate_on_update, :validate
I was searching for these amongst MongoMapper doucmentation and blog posts when really they are an ActiveSupport concept as the following comment suggests:
Almost all of this callback stuff is pulled directly from ActiveSupport . . .
Being new to rails I wasn’t very familiar with the ActiveSupport stuff and so I didn’t know to look there. Anyway this is how I use them in my posts model:
class Post
include MongoMapper::Document
many :comments
key :title, String, :required => true
key :slug, String
key :body, String, :required => true
key :published, Boolean
key :published_on, Date, :default => Date.today
after_save :update_comment_titles
private
def update_comment_titles
comments.each do |comment|
comment.post_title = self.title
comment.save
end
end
end
My comments live in a separate document collection and each Comment document contains the title of the Post that it belongs to. This allows me to list all comments (on the admin page for example) without having to query any of the Post documents.
This above after_save call back ensures that the post_title on each Comment is updated if I ever update the title of the post.



These common settings are now ready to be used by any project in the solution by following these steps:


