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
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
ya just did. DOH!!
great!
Thank you so much 🙂
config/initializer/mongo.rb
is mistake!
config/initializers/mongo.rb
is correct
Thank you – I have corrected the example
before bundle install
I did bundle update
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!