Archive for the ‘ rails31 ’ Category

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
Advertisements