Skip to content
Implements optimistic locking (similar to ActiveRecord) for MongoMapper
Find file
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


mm-optimistic_locking implements optimistic locking in mongo_mapper.

Optimistic Locking?

When attempting to save a record, optimistic locking verifies that the record has not been modified since it was earlier loaded into memory. If it has been modified and resaved since that time, an error is raised and the record must be reloaded and resaved again.

Optimistic locking is appropriate when the chance of a conflict is low and records can easily recover when a conflict does occur (e.g., simply by retrying the operations or by asking an end user to manually resolve the conflict).

Before using mm-optimistic_locking, you should first consider if your use case for locking can be handled by MongoDB's various atomic operations. It is possible that you do not need this type of locking at all.


If using Bundler (e.g., with Rails 3), add the gem to your project's Gemfile:

gem 'mm-optimistic_locking'

And run bundle install.

Next, include MongoMapper::Plugins::OptimisticLocking in each model that needs optimistic locking functionality.

class BlogPost
  include MongoMapper::Document

  # Add the following line to each model that needs optimistic locking
  plugin MongoMapper::Plugins::OptimisticLocking

Finally, make sure to rescue from MongoMapper::StaleDocumentError any time you save a model with optimistic locking functionality:

rescue MongoMapper::StaleDocumentError
  # Reload, remodify, and resave

Related Projects


  • Fork the repository on GitHub
  • Create a feature branch (a.k.a., create a branch named my-awesome-feature)
  • Write the new feature (please also add specs)
  • Make sure all specs pass: rake spec
  • Submit a pull request that only includes your feature branch
Something went wrong with that request. Please try again.