Guide to install MongodB on Ubuntu 14.04 and setting up Rails 5 with Mongoid gem on New and Old Project

Kartik Jagdale
Aviabird
Published in
5 min readDec 18, 2016

Overview

In this guide we will see how to install MongodDB on linux and how to setup rails 5 with Mongoid gem. If you haven’t checked MongoDB yet I will encourage you to check it out. It is a is a free and open-source NoSQL document database used commonly in modern web applications.

Installing MongoDB

I am currently using ubuntu 14.04 as my operating system and I will quickly walk you through how to install MongoDB on ubuntu 14.04.

If you have already Installed MongoDB, you might want to skip to Setting up Rails 5 with Mongoid step of this article.

Step 1: Importing a Public Key

Here we will import MongodDB GPG public key. GPG public Key is needed so that ubuntu is able to ensure the authenticity of a software package by verifying that they are signed with GPG keys.

To do Execute this.

sudo apt-key adv — keyserver hkp://keyserver.ubuntu.com:80 — recv 7F0CEB10

Step 2: Adding MongoDB repo details to package list

Here we will add the MongodDB repository details so our package manager knows from where to download the packages from.

Execute the following command to create a list file for MongoDB.

echo “deb http://repo.mongodb.org/apt/ubuntu “$(lsb_release -sc)”/mongodb-org/3.0 multiverse” | sudo tee /etc/apt/sources.list.d/mongodb-org-3.0.list

Also we will need to update the package list

sudo apt-get update 

Step 3: Installing and Verifying MongodDB

To install mongodb, execute the following command.

sudo apt-get install -y mongodb-org

This will install mongodb and to verify the installation run the following command

service mongod status

If MongodDB is running, you’ll see following output, of course with different process ID.

mongod start/running, process 1611

Further you can start, stop, restart MongoDB using the service command.

E.g: sudo service mogodb stop and sudo service mongodb start .

Setting up Rails 5 with Mongoid

Now that we have installed MongoDB we are up and ready to setup rails with Mongoid gem. Just that you know, Mongoid is an ODM (Object-Document-Mapper) framework for MongoDB in Ruby.

Now there can be two scenarios here.

  1. You are yet to install mongoid and you are about to generate rails project from scratch.
  2. You have already generated a rails project the traditional way i.e rails new app-name and now want to install mongoid. (This Step is a tricky one)

Rails 5 with Mongoid (New Project)

Here we need to generate a rails application with

rails new app-name --skip-active-record 

Now here --skip-active-record is important because it doesn’t include ActiveRecord in the app that is generated. Just for your informationActive Record connects classes to relational database tables to establish an almost zero-configuration persistence layer for applications. But here we want to connect to MongoDB which is a Non-relational database. Hence the skip command

Further Just in case if you are setting up rails in API mode, you probably would want to add --api in the above command.

Once the app is generated, we need to remove the sqlite3 and add Mongoid.

Search and delete the below line in your Gemfile:

# Use sqlite3 as the database for Active Record 
gem ‘sqlite3’

and add this line:

gem 'mongoid', '~> 6.0'

Once you have added, run bundle to install gem in your Rails app.

Now we need to generate the Mongoid Configuration file that is just like our config/database.yml that we use with ActiveRecord.

To generate mongoid config file, execute the following command.

rails generate mongoid:config

This will generate config/mongoid.yml a configuration file for MongoDB.

And Hurray we have successfully setuped Rails 5 with Mongoid.

Rails 5 with Mongoid (Old Project)

Now this is where it gets tricker.

You already have generated a rails project with active record and now you want to install mongoid.

You install it with the above method and then start getting errors when trying to generate models or even a generating a mongoid.yml file, because of scattered acrive_record references lying around.

Trust me I have faced this issue and wasted a lot of time in fixing errors.

So, here I am going to show you how to install mongoid and perform some additional configurations and clean ups in old projects to avoid future errors. Let’s do it.

Step 1:

Just as above Add mongoidgem to your gemfile and bundle

Step 2:

Generate mongoid configuration just like in above procedure done for new project.

Step 3:

Now, you probably might have created a lot of models and associations in your project (Hopefully not a lot) and each models might have lot of columns.

But most of the time all those references are either in migration’s or database schema file and you want all those details in your new MongoDB database. And as we all know that when we are using mongoid we write all these relationships and column names in its respective model file itself.

So, we need to write all our column names and its associations of models in our application to their individual model file in ‘Mongoid’ Style.

To do so you probably need to read some of the mongoid documentation on their official website on how write field names and associations.

Further, If you have a lot of columns in your model and you don’t want to jump back and forth from your database console to your rails model file to replicate all the columns and associations in mongodb, we have a cool gem called annotate which adds a beautiful comment summarizing the current schema to the top or bottom of each of your activerecord models like below example. Which will make our lives much easier.

Which will make our lives much easier.

# == Schema Info
#
# Table name: line_items
#
# id :integer(11) not null, primary key
# quantity :integer(11) not null
# product_id :integer(11) not null
# unit_price :float
# order_id :integer(11)
#

Step 3:

Now we want to clean up all active_record references in our rails project.

First we will find all the references using below command.

grep -r active_record config/

You’ll get a list of all files where active_record was mentioned.

Open those files individually and remove or comment out the active_record references.

Step 4:

Remove database adapter gems from your Gemfile like mysql2, sqlite3, etc

Further delete or backup your config/database.yml db/schema.rb and migrations (if any) from the application.

In short you just have to erase all things associated with active_record.

Step 5:

Run your rails application and check if everything is working probably.
And Again Hurray we have successfully setuped Rails 5 with Mongoid on your Old Project.
If you run into any problems while following above procedure, please comment below, I’l try my best to resolve your queries.
See Ya!!!

--

--