Forward Advance Learning

Rails Integration with Mongoid (optional)

In this optional section we will look at how to connect a Mongo database to a Rails app. Skip this if you don't plan on using Mongo with Rails.

The official Rails/Mongo integration is a gem called Mongoid. This gem sits alongside ActiveRecord, and allows you to talk to Mongo and get regular Ruby objects back.

What it gives you

  • Generators, so you can make Mongo objects
  • Application tier validation
  • The lucid API for making simple queries
  • Access to the aggregate framework
  • Access to Map Reduce (though you still need to write JavaScript for this)

Installing Mongoid

First of all, create an application. Lets make a petstore:

rails new pet_store

Then add the Mongoid gem to the Gemfile

gem 'mongoid'

now bundle to get the new gem:


You don't need to remove your existing database gem, though you can if you wish. Mongo is happy to sit alongside ActiveRecord with SQLLite or Postgres.

At time of writing (September 2016) Mongoid is compatible with Rails 4 and 5, so choose your flavour.

One final step, you need a Mongoid configuration file. Make this by running:

rails g mongoid:config

You now have a new config file called config/mongoid.yml where you can configure your production database URL.

That's it

Mongoid Generator

Your first step to mongoid integration is to make yourself a model. Mongoid overrides the default generators, so you can just create a model as you would normally:

rails g model kitten name:string age:integer

The model that gets generated looks like this:

class Kitten
include Mongoid::Document
field :name, type: String
field :age, type: Integer

The Scaffold generator also works. You can run:

rails g scaffold Hamster name:string fluffy_bits:integer

CRUD actions.

You can do all the standard things you might expect, for example

Finding by id. The ID will be a Mongo ID string.

@toast = Toast.find(params[:id])

Creating and saving:

@toast ={jam:true})


@toast = @toast.update(toast_params)

And destroying:


Exercise - CRUD

Create a Rails project, and add the mongoid gem. Scaffold a resource. Now start the server and have a play with what you made.


Mongoid uses the same validators as ActiveRecord, we can write things like:

validates_presence_of :name
validates_uniqueness_of :email
validates_numericality_of :age

We also get the generic validate method that will accept the name of a function.

validate :person_is_older_than_cat
def person_is_older_than_cat
this.age > this.cat_age

Exercise - Validation

Add a sensible validation to your scaffolded model.


Active record gives us relationships between models using a relational database. Mongo gives us similar features, but because it's a document storage engine, we use embedding.

We embed using embeds_many, or embeds_one.

Embeds many will give you an array that you can write to.

class Address
include Mongoid::Document
embedded_in :kitten
class Kitten
include Mongoid::Document
embeds_many :addresses

We can then set addresses:

kitten.addresses.create = address_params

To get the addresses back, we can just call:


Exercise - relations

Extend your application. Scaffold a second resource. Embed your second resource in the first.

Create a simple UI that allows you to create your nested resource inside your parent resource.

If you get stuck, refer to the documentation here:


We can get all the items in the collection with the all method:


We can query the relation by passing a Hash

For example:

Kitten.where( name: 'Mittens' );

We compose more complex queries by modifying the hash key:

Kitten.where( => 2 )

Exercise - Queries

Create a controller and a view that allows you to search for by some criteria that will be passed in in the URL.

This will give you a list of matched items.