Skip to content

Instantly share code, notes, and snippets.

@casidiablo
Created December 30, 2011 18:39
Show Gist options
  • Save casidiablo/1540960 to your computer and use it in GitHub Desktop.
Save casidiablo/1540960 to your computer and use it in GitHub Desktop.
Steps to create a Rails application with MongoDb

This document contains the steps to create a mongo-enabled Rails 3.1 application. This is just for personal reference actually.

Create base application

We have to skip the Active Record stuff:

rails new app_name --skip-active-record

Add mongo and bson_ext to the Gemfile

We are using mongo_mapper... though mongoid looks good to. So, we have to add the mongo_mapper gem and the bson_ext for performance:

gem 'mongo_mapper'
gem 'bson_ext'

Then run bundle install as usual.

Configure mongo

First, let's create a configuration file by executing this command:

rails generate mongo_mapper:config

Command above will create a config/mongo.yml file. That's it... just awesome!

Run Rails and Mongo Daemon using foreman

Create a Procfile like this:

mongo: mongod
rails: rails server

Then just use the foreman start command to start both services.

Heroku configuration

It is important to create the Heroku app in the Cedar stack. Not sure why it does not work on the Bamboo one. So you must use:

heroku create --stack cedar app-name

To create a MongoLabs account:

heroku addons:add mongolab:starter

Then, just use the following command to get the connection Uri:

heroku config | grep MONGOLAB_URI

Using the Uri information you must edit the config/mongo.yml file in the production section. For more information or steps to create/restore backups refer to the official documentation.

Enable HAML generator

If using HAML, add haml-rails gem to your Gemfile:

gem 'haml-rails'

And add this to the config/application.rb file:

config.generators do |g|
  g.template_engine :haml
end

Generating scaffolds

Here you just have to specify the ORM to use (shouldn't be ODM? whatever):

rails generate scaffold model_name name:string --orm=mongo_mapper

Generate simple models

rails generate mongo_mapper:model model_name field:type field:type
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment