Docs Menu

Rails Integration

In this guide, you can learn about features that are automatically enabled when you use Mongoid in a Ruby on Rails application. This guide also describes Rails-related functionality that you can enable in your application.

You can configure Mongoid-specific options and other Rails-environment specific options in your main application file by accessing config.mongoid. The mongoid:config generator creates an initializer in the config/initializers/mongoid.rb file.

Note

Any options set in your config/mongoid.yml file take precedence over options set elsewhere. For this reason, use mongoid.yml as the default location for Mongoid configuration when possible.

To learn more about all available configuration options, see the Configuration guides.

The following code demonstrates how to create a Rails logger by accessing config.mongoid:

module MyApplication
class Application < Rails::Application
config.mongoid.logger = Logger.new(STDERR, :warn)
end
end

To learn more about logging settings, see the Logging Configuration guide.

To set up single collection inheritance, Mongoid must preload all models before every request in development mode. This can slow down your application, so if you are not using any inheritance you can turn this feature off.

The following code demonstrates how you can turn off preloading by setting the preload_models feature to false:

config.mongoid.preload_models = false

Similar to Active Record, Mongoid configures Rails to automatically convert certain exceptions to HTTP status codes. The following list provides the conversions between Mongoid exceptions and HTTP codes:

  • Mongoid::Errors::DocumentNotFound: Converted to 404 Not Found

  • Mongoid::Errors::Validations: Converted to 422 Unprocessable Content

Mongoid can output the time spent executing database commands to the Rails instrumentation event process_action.action_controller. Mongoid obtains these values through driver command monitoring. You application logs this time amount with view time as shown in the following output:

Completed 200 OK in 2739ms (Views: 12.6ms | MongoDB: 0.2ms)

This logging is set up automatically in your Rails application.

Note

Time Calculation

The time indicated in log entries is the time that the MongoDB deployment takes to run MongoDB operations in addition to the time taken to send commands and receive results from MongoDB Server. It does not include time taken by the driver and Mongoid to generate the queries, cast types, or otherwise process the results.

You can use following rake tasks for Mongoid when using the Rails framework:

  • db:create_indexes: Reads all index definitions from the models and attempts to create them in the database

  • db:remove_indexes: Removes indexes for each model

  • db:drop: Drops all collections in the database except for system collections

  • db:purge: Deletes all data, including indexes, from the database

  • db:seed: Seeds the database from the db/seeds.rb file

  • db:setup: Creates indexes and seeds the database

The following rake tasks exist only for framework dependency purposes and do not perform any actions:

  • db:test:prepare

  • db:schema:load

  • db:create

  • db:migrate

To learn about how to set up a new Rails application that uses Mongoid, see the Quick Start - Ruby on Rails guide.

To learn how to add Mongoid to an existing Rails application, see the Rails Application section of the Add Mongoid to an Existing Application guide.