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://github.com/collectiveidea/devise-mongo_mapper'

group :test, :development do
  [whatever testing gems you want in here]
end
 
 
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(File.read(File.join(Rails.root, "/config/mongodb.yml")))

if db_config[Rails.env] && db_config[Rails.env]['adapter'] == 'mongodb'
  mongo = db_config[Rails.env]
  MongoMapper.connection = Mongo::Connection.new(mongo['host'] || 'localhost',
                                                 mongo['port'] || 27017,
                                                :logger => Rails.logger)
  MongoMapper.database = mongo['database']
  
  if mongo['username'] && mongo['password']
    MongoMapper.database.authenticate(mongo['username'], mongo['password'])
  end
end
 
  
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
  host: genesis.mongohq.com
  username: your-username
  password: your-password
development:
  <<: *base
test:
  <<: *base
  database: coolapp-test
production:
  <<: *base
 

Comments

Popular posts from this blog

Inserting and Moving elements inside Ruby Array

Difference between Validations, Callbacks and Observers