How to configure Rails App to use Mongodb

In this post i will show you guys how to make your rails application to talk with Mongodb.

Mongodb is a nosql database which stores data as document.

Inorder to configure your rails app with you need to follow the following steps:

Step 1 :

create a rails application as

$rails new my_app –skip-active-record   or     $ rails new my_app  -O -T

in the above step we skip Active Record which is the default ORM layer in Rails.

 

STEP 2:

Now enter the following gems in your gem file

  1. gem ‘mongoid’, ‘2.0.0.beta.19’
  2. gem ‘bson_ext’

$ bundle install

STEP 3 :

Now you successfully installed all the gems required by the app to interact with the mongodb database.

$ rails  g  mongoid:config

it will create configuration file in  config/mongoid.yml

STEP 4 :

generate a model to test your app whether it is connected to mongodb or not

$ rails g model article name:string content:text

and go to your model file and see that it looks like this

class Article  
  include Mongoid::Document  
  field :name, :type => String  
  field :content, :type => String  
end 

 

so thats it you configured your app with mongodb.


 

 

 

 

 

Leave a comment