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 http://www.mongodb.org/display/DOCS/Quickstart+OS+X

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

mongod

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

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

lib/tasks/mongo.rake

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

Configure your app

Gemfile

source 'http://rubygems.org'

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
end

config/application.rb

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"
    end

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)

    suggestions?

    • 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.

      Best,
      ++Robert

  2. ya just did. DOH!!

  3. great!

  4. Thank you so much :)

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

    config/initializers/mongo.rb
    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)
        happend

        bundle show mongo_mapper
        /Users/username/.rvm/gems/ruby-1.9.3-p194/gems/mongo_mapper-0.12.0

        I have!

  1. December 21st, 2011

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 )

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

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: