Getting Rails 3.1, mongodb and scaffold generators to work.

This tutorial is using rvm and homebrew to show how to install a fully functional rails 3.1 application on mongodb.

Install mongodb

Extracted from

brew install mongodb

sudo mkdir /data/db

sudo chown -R username /data

The last step is probably not necessary, but I like being able to access all data dirs as my current user


Start mongo server

Install ruby 1.9.2, and rails 3.1.0.rc4

rvm install 1.9.2

rvm gemset create rails31

rvm gemset use rails31

gem install rails --pre

Create new rails app

rails new app_name --skip-active-record
cd app_name

echo "rvm 1.9.2@rails31" > .rvmrc

The .rvmrv will automatically switch to proper ruby and gemset when entering the dir.

Setup mongo

Most of the steps are taken from the great mongo guide


MongoMapper.connection ='localhost', 27017)
MongoMapper.database = "#myapp-#{Rails.env}"

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


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

Configure your app


source ''

gem 'rails', '3.1.0.rc4'

# Asset template engines
gem 'sass-rails', "~> 3.1.0.rc"
gem 'coffee-script'
gem 'uglifier'

gem 'jquery-rails'

# mongo drivers
gem 'mongo_mapper'
gem 'bson_ext'

group :test do
  gem "rspec"
  gem "rspec-rails"
  gem "factory_girl"
  # Pretty printed test output
  gem 'turn', :require => false


Add inside of class Applicaion < Rails::Application. This will make the generators work

   config.generators do |g|
      g.orm :mongo_mapper    # :active_record
      g.template_engine :erb # :haml
      g.test_framework :rspec, :fixture => true, :views => false
      g.fixture_replacement :factory_girl, :dir => "spec/factories"

Run it all

bundle install

rake test

rails g scaffold User

rake s
  1. Howdy, i am getting this error doing bundle:

    Bundler could not find compatible versions for gem “activesupport”:
    In Gemfile:
    mongo_mapper depends on
    activesupport (~> 3.0.0)

    rails (= 3.1.0.rc5) depends on
    activesupport (3.1.0.rc5)


    • The mongo_mapper guys are probably going to release a version compatible with rails 3.1 soon. If you can’t wait, however, I would suggest forking mongomapper on github and changing mongo_mapper.gemspec to depend on activerecord 3.1.0. Given that rails 3.1 is not breaking compatibility with mongo_mapper, there is a good chance that changing gemspec will help.


  2. ya just did. DOH!!

  3. great!

  4. Thank you so much :)

  5. config/initializer/mongo.rb
    is mistake!

    is correct

  6. before bundle install

    I did bundle update

  7. rake aborted!
    undefined method `groups’ for Rails:Module

    • I commented out
      uninitialized constant MongoMapper (NameError)

      • However
        uninitialized constant MongoMapper (NameError)

        bundle show mongo_mapper

        I have!

  1. December 21st, 2011

Leave a Reply

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

You are commenting using your 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 )

Google+ photo

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

Connecting to %s


Get every new post delivered to your Inbox.

%d bloggers like this: