Rails Considerations¶
On this page
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 | ✓ | ✓ | ||
Mongoid 6.4 | ✓ | ✓ | ||
Mongoid 6.3 | ✓ | ✓ | ||
Mongoid 6.2 | ✓ | ✓ | ||
Mongoid 6.1 | ✓ | |||
Mongoid 6.0 | ✓ | |||
Mongoid 5.4 | ✓ | |||
Mongoid 5.2 | ✓ |
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.
Railties¶
Mongoid provides some railties and initializers that one should be aware of when writing a Rails application with Mongoid.
Configuration¶
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
.
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.
Exceptions¶
Similar to Active Record, Mongoid tells raise to return specific http codes when some errors are raised.
Controller Runtime Instrumentation¶
Mongoid provides time spent executing MongoDB commands (obtained via a driver command monitoring subscription) to Rails’ instrumentation event process_action.action_controller. This time is logged together with view time like so:
This logging is set up automatically.
Note: the time indicated is the time taken by MongoDB cluster to execute MongoDB operations, plus the time taken to send commands and receive results from MongoDB over the network. It does not include time taken by the driver and Mongoid to generate the queries or type cast and otherwise process the results.
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.0db:schema:load
: Exists only for framework dependency purposes, does not actually do anything.db:seed
: Seeds the database from db/seeds.rbdb:setup
: Creates indexes and seeds the database.db:test:prepare
: Exists only for framework dependency purposes, does not actually do anything.