MongoDB with Ruby on Rails

Rahul Kanholkar
Simform Engineering
3 min readSep 14, 2023

Learn how to integrate MongoDB into a Ruby on Rails web application.

MongoDB is a popular choice among developers partly because it permits a one-to-one mapping between object-oriented (OO) software objects and database entities. Ruby developers are at a great advantage in using MongoDB because they are already used to working with and designing software that is purely object-oriented.

Most of the discussions I’ve had about MongoDB and Ruby assume Ruby knowledge and explain why MongoDB is a good fit for the Rubyist. This post will do the opposite; I will assume you know a few things about MongoDB but not much about Ruby. In showing the Rubyist’s OO advantage, I’ll share a bit about the Ruby programming language and its popularity, explain specifically how the majority of Ruby developers are using MongoDB, and then talk about the future of the 10gen Ruby driver in the context of the Rails community.

If you have not installed MongoDB on your machine, you first need to install it. Here are the steps:

$ sudo apt-key adv - keyserver hkp://keyserver.ubuntu.com:80 - recv 2930ADAE8CAF5059EE73BB4B58712A2291FA4AD5
$ echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.6 multiverse" | sudo tee
/etc/apt/sources.list.d/mongodb-org-3.6.list
$ sudo apt-get update
$ sudo apt-get install -y mongodb-org

Check the below commands for MongoDB

To start MongoDB, use:

$ sudo service mongod start

To stop MongoDB, use:

$ sudo service mongod stop

To restart MongoDB, use:

$ sudo service mongod restart

Let's create a new rails application with MongoDB.

$ rails new my_mongo_app --skip-active-record

We skip ActiveRecord during the Rails app creation because MongoDB doesn’t use it.

Next, add the following gems to your Gemfile:

gem 'mongoid', '~> 6.0'
gem 'bson_ext'

After that, run bundle install. Now, let’s generate themongoid.yml file, which is similar to the database.yml file for us.

Run the following command to generate MongoDB configuration files:

rails g mongoid:config

Now update mongoid.yml file based on your MongoDB configurations and create a database using rake db:create. Mongoid provides these generators to manage the database with these rake tasks.

development:
clients:
default:
database: blog_development
hosts:
- localhost:27017
options:
server_selection_timeout: 1

Let’s add a Post model with a few fields:

bin/rails g scaffold Post title:string body:text

Also, add a Comments model with a few fields:

bin/rails g scaffold Comment name:string message:string post:belongs_to

Here’s how the Post and the Comment models look.

class Post
include Mongoid::Document

field :title, type: String
field :body, type: String

has_many :comments, dependent: :destroy
end



class Comment
include Mongoid::Document

field :name, type: String
field :message, type: String

belongs_to :post
end

These are the basic steps that we need to follow to integrate MongoDB with Rails Application.

Conclusion

Interacting with your MongoDB data via Ruby — either using the Driver or the ODM — is straightforward, but you can also directly interface with your data from MongoDB Atlas using the built-in Data Explorer. Depending on your preferences, though, there are options:

  • MongoDB for Visual Studio Code allows you to connect to your MongoDB instance and enables you to interact in a way that fits into your native workflow and development tools. You can navigate and browse your MongoDB databases and collections, and prototype queries and aggregations for use in your applications.

Keep following the Simform Engineering publication to stay in the know of the latest trends in the development ecosystem.

Follow Us: Twitter | LinkedIn

--

--