Getting Started With MongoMapper and Rails
Warning: This is currently out of date as it was based on an older version of MongoMapper.
I have had a few requests for tips on getting started with MongoDB and Rails so I thought I would drop some quick knowledge. It is actually really easy to get going, but Rails always does everything for you so when something comes along that doesn’t, you sometimes feel lost.
rails mongomapper_demo
cd mongomapper_demoNow that your Rails shell is created, let’s add MongoMapper to the mix. Open up environment.rb. First, configure it as a gem, then remove ActiveRecord from the mix, and lastly, point it at a database.
Rails::Initializer.run do |config|
config.gem 'mongomapper', :version => '>= 0.2.1'
config.frameworks -= [:active_record]
end
MongoMapper.database = "myappname-#{Rails.env}"As of now there are no fancy generators for your models (gasp!) so you can just create a new file in app/models and an accompanying file in test/unit. Or, if you like, you can use script generate like so and just adjust your model after it is created (until I or some kind soul gets around to generators).
script/generate model Note --skip-migrationThen you can just change your app/models/note.rb file to be something like this:
class Note
include MongoMapper::Document
key :title, String
key :body, String
endI’d like to say there is more to it, but there isn’t. :) Don’t worry about creating your database or migrating it. It all happens on the fly.
Video
For those that prefer visual learning, I’ve even whipped together a short screencast where I install MongoDB on OSX, fire it up, and build a really basic note app. Enjoy!



Recent comments
1 week 3 days ago
1 week 4 days ago
3 weeks 3 days ago
5 weeks 5 days ago
20 weeks 3 days ago
23 weeks 3 days ago
24 weeks 1 day ago
24 weeks 1 day ago
24 weeks 3 days ago
26 weeks 5 days ago