Computing Magazine

MongoDB Integration with RAILS

Posted on the 29 April 2014 by Abhishek Somani @somaniabhi
This blog post lists step by step instructions for integrating MongoDB in RAILS Application . STEP 1:To create application with Mongo db you need to skip active record.Type command

rails new app_name -–skip-active-record
STEP 2:Before bundle install make sure that you have included following gems in your gemfile.

gem 'mongoid'
gem 'bson_ext'
run bundle intsall
IF any problem occurs during bundle installation remember dependencies of mongoid should be of same version.Otherwise it will give error while installing bundle.Now you can see that dependencies of gem 'mongoid' are of same version in snapshot. gem 'mongo' '1.6' gem 'bson' '1.6' gem 'bson_ext' '1.6' STEP 3:Now run the server check whether connection established with mongoid.MongoDB Integration with RAILS Mongoid.yml is not found. STEP 4:Let's create mongoid.yml file .

rails g mongoid:config
MongoDB Integration with RAILS STEP 5:Let's generate scaffold,here we dont need to mention skip active record though it is already mentioned. Type command

rails g scaffold article name:string content:text
When you will check your model you will find instead of

class Article<ActiveRecord::Base
class Article
include Mongoid::Document
Now instead of generating migration we will write fields in model itself.

class Article
include Mongoid::Document
field :name,:type => String
No need of migration file in mongo db. MongoDB Integration with RAILS Now Start the Server . STEP 5:Validation and Association Validation is same as it was in ActiveRecord but Associations is slightly different.Some new association are introduced.

embeds_many and embedded_in,embeds_one and embedded_in,references_many and referenced_in,references_one and referenced_in and associations used in active record.
Post Comments And Suggestions !!! Thanks to Santosh for writing this post .

You Might Also Like :

Back to Featured Articles on Logo Paperblog

These articles might interest you :