July 23, 2009

Posted by John

Tagged gems, mongodb, mongomapper, and video

Older: Uploadify and Rails 2.3

Newer: Patterns Are Not Scary: Method Missing Proxy

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_demo

Now 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-migration

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

I’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!

MongoMapper Demo

9 Comments

  1. Just started playing with MongoMapper, and I love it. However one minor glitch with your demo, fella. Version 0.2.1 isn’t available on github or rubyforge, what are you hiding from us? Let it out! ;-)

  2. Dang! Well, just go with 0.2.0 for now and I’ll try to finish the last features of 0.2.1 by early next week. :)

  3. I quickly threw a model generator together: http://github.com/stympy/mongo-generators/tree/master

  4. @Ben – Sweet! Can’t wait to have time to get my hands on that.

  5. Oh the dilemma, another document DB to play with!

  6. Why do you disable ActiveRecord?

    The reason I ask is that I’d like to use a local sqlite db to store admin information and get mongo (like the blazing saddles mongo) to do the heavy lifting because it can handle it.

    Is this possible? I’m about to knock something up to try it but thought it would be wise to ask as well.

  7. @mhussa yeah that will work fine. I disable because I don’t use it.

  8. Excellent, thankyou for mongomapper and for evangelising mongodb. I Wouldn’t have known about it if it hadn’t been for your tips.

  9. James H James H

    Aug 03, 2009

    Your screencast appears to be dead. Would you mind reposting it?

Sorry, comments are closed for this article to ease the burden of pruning spam.

About

Authored by John Nunemaker (Noo-neh-maker), a programmer who has fallen deeply in love with Ruby. Learn More.

Projects

Flipper
Release your software more often with fewer problems.
Flip your features.