Rails Considerations

Mongoid was built and targeted towards Rails applications, even though it will work in any Ruby application. This document explains how to use Mongoid with a Rails application.

Rails Version Compatibility

Refer to the following compatibility matrix to determine which version of Mongoid to use with your Rails application.

Mongoid Version Rails 5.2 Rails 5.1 Rails 5.0 Rails 4.2
Mongoid 7.0 Yes Yes No No
Mongoid 6.4 Yes Yes No No
Mongoid 6.3 Yes Yes No No
Mongoid 6.2 Yes Yes No No
Mongoid 6.1 No No Yes No
Mongoid 6.0 No No Yes No
Mongoid 5.4 No No No Yes
Mongoid 5.2 No No No Yes

Using Mongoid with a new Rails application

If you are creating a new Rails application, you’ll have to use the --skip-active-record flag with rails new.

Using Mongoid with an existing Rails application

If you’re starting to use an existing Rails application with Mongoid, you’ll have to update your config/application.rb file to remove the require 'rails/all' line and only include the frameworks you want. You must also ensure that you remove all references to ActiveRecord in the config directory and models directory.


Mongoid provides some railties and initializers that one should be aware of when writing a Rails application with Mongoid.


You can set Mongoid configuration options in your application.rb along with other Rails environment specific options by accessing config.mongoid. Options set here will override those set in your config/mongoid.yml.

module MyApplication
  class Application < Rails::Application
    config.mongoid.logger =$stdout, :warn)

Model Preloading

In order to properly set up single collection inheritance, Mongoid needs to preload all models before every request in development mode. This can get slow, so if you are not using any inheritance it is recommended you turn this feature off.

config.mongoid.preload_models = false


Similar to Active Record, Mongoid tells raise to return specific http codes when some errors are raised.

Mongoid::Errors::DocumentNotFound : 404
Mongoid::Errors::Validations : 422

Rake Tasks

Mongoid provides the following rake tasks when used in a Rails 3 environment:

  • db:create: Exists only for dependency purposes, does not actually do anything.
  • db:create_indexes: Reads all index definitions from the models and attempts to create them in the database.
  • db:remove_indexes: Reads all secondary index definitions from the models.
  • db:drop: Drops all collections in the database with the exception of the system collections.
  • db:migrate: Exists only for dependency purposes, does not actually do anything.
  • db:purge: Deletes all data, including indexes, from the database. Since 3.1.0
  • db:schema:load: Exists only for framework dependency purposes, does not actually do anything.
  • db:seed: Seeds the database from db/seeds.rb
  • db:setup: Creates indexes and seeds the database.
  • db:test:prepare: Exists only for framework dependency purposes, does not actually do anything.
←   Indexes Upgrading Mongoid  →