Create New Rails Application with MongoDB

Create a New Rails Application with MongoDB
# Use sudo if your setup requires it
gem install rails
Create New Rails Application by skipping the Active Record
rails new awesome_app --skip-active-record 
This is the gem file and the list of the required gems for MongoDB

require 'rubygems'
require 'mongo'

source :rubygems

gem 'mongo_mapper'
gem 'rails', '3.0.3'
gem 'devise', '1.1.3'
gem 'devise-mongo_mapper',
  :git    => 'git://'

group :test, :development do
  [whatever testing gems you want in here]
Then Run bundle install to install all the gems mentioned in the gem file.
$ bundle install
There's two initializer files we'll need. We add the one for mongo,  which I put in config/initializers/mongo.rb:
# config/initializers/mongodb.rb

include MongoMapper

db_config = YAML::load(, "/config/mongodb.yml")))

if db_config[Rails.env] && db_config[Rails.env]['adapter'] == 'mongodb'
  mongo = db_config[Rails.env]
  MongoMapper.connection =['host'] || 'localhost',
                                                 mongo['port'] || 27017,
                                                :logger => Rails.logger)
  MongoMapper.database = mongo['database']
  if mongo['username'] && mongo['password']
    MongoMapper.database.authenticate(mongo['username'], mongo['password'])
This is the Structure of the Mongodb.yml file.
# config/mongodb.yml
base: &base
  adapter: mongodb
  database: coolapp
#These are needed to authenticate with your db
#should it run on another server
  username: your-username
  password: your-password
  <<: *base
  <<: *base
  database: coolapp-test
  <<: *base


Popular posts from this blog

Interview Question on Ruby on Rails

Inserting and Moving elements inside Ruby Array