How to integrate MongoDB in rails 3.X

Mongodb

  • MongoDB permits a one-to-one mapping between object-oriented (OO) software objects and database entities.
  • MongoDB is a document database that focuses on developer needs.
  • MongoMapper, a project by Jon Nunemaker from Github, is a simple ORM for MongoDB
  • Mongoid, in particular, has become quite popular since its creation 4 years ago by Durran Jordan. Mongoid’s goal is to provide a familiar API to Active Record.

Installation is very easy…only make sure when you are creating a new rails application you are not loading ActiveRecord.

Assuming that you have already installed rails in your system.

Step-1

The important thing here is to avoid loading ActiveRecord. One way to do this is with the –skip-active-record switch. So you’d create your app skeleton like so:

> rails new my_app –skip-active-record

Alternatively, if you’ve already created your app (or just want to know what this actually does), have a look at config/application.rb and change the first lines from this:

require “rails/all”

to this:

require “action_controller/railtie”
require “action_mailer/railtie”
require “active_resource/railtie”
require “rails/test_unit/railtie”

It’s also important to make sure that the reference to active_record in the generator block is commented out:

# Configure generators values. Many other options are available, be sure to check the documentation.
# config.generators do |g|
# g.orm :active_record
# g.template_engine :erb
# g.test_framework :test_unit, :fixture => true
# end

As of this writing, it’s commented out by default, so you probably won’t have to change anything here.

Bundle and Initialize

The final step involves bundling any gems you’ll need and then creating an initializer for connecting to the database.

Bundling

Normally, using MongoDB will simply mean adding whichever OM framework you want to work with, as these will require the “mongo” gem by default.

# Edit this Gemfile to bundle your application’s dependencies.

source ‘https://rubygems.org’

gem “rails”, “3.0.0”

gem “mongo_mapper”

However, there’s currently an issue with loading bson_ext, as the current gemspec isn’t compatible with the way Bundler works

In the meantime, you can use the following work-around:

# Edit this Gemfile to bundle your application’s dependencies.

require ‘rubygems’
require ‘mongo’

source ‘https://rubygems.org’

gem “rails”, “3.0.0”
gem “mongo_mapper”

Requiring rubygems and mongo before running the gem command will ensure that bson_ext is loaded. If you’d rather not load rubygems, just make sure that both mongo and bson_ext are in your load path when you require mongo.

Once you’ve configured your Gemfile, run the bundle installer:

> bundle install

Initializing

Last item is to create an initializer to connect to MongoDB. Create a Ruby file in config/initializers. You can give it any name you want; here we’ll call it config/initializers/mongo.rb:

MongoMapper.connection = Mongo::Connection.new(‘localhost’, 27017)
MongoMapper.database = “#myapp-#{Rails.env}”

if defined?(PhusionPassenger)
    PhusionPassenger.on_event(:starting_worker_process) do |forked|
        MongoMapper.connection.connect if forked
    end
end

 Running Tests

A slight modification is required to get rake test working (thanks to John P. Wood). Create a file lib/tasks/mongo.rake containing the following:

namespace :db do
   namespace :test do
      task :prepare do
          # Stub out for MongoDB
      end
  end
end

Now the various rake test tasks will run properly.

Hope it will help you..If you still find any difficulty…feel free to ask me..

Thanks.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s