Kevin’s Can

« Back to the Can

MongoMapper and Rails 3-pre

Update: MongoMapper now works out of the box with Rails 3, thus the steps in this post are no longer needed. Happy hacking.

 

Although the usage of MongoMapper is straightforward, it takes some extra attention to get it running on an edgy Rails 3 (rails-3.0.pre) app. I followed three steps to get it working for me.
Keep in mind that these steps are needed right now as I write this post. I'm sure that all this will work out of the box when Rails 3 and MongoMapper are stable.

First off, remove ActiveRecord dependency from config/boot.rb file:

Otherwise you'll get an error saying that ActiveRecord cannot establish a connection.

Step 2. Point the MongoMapper gem source to its Github repository. This is done in the Gemfile:

Re-bundle:
$ gem bundle

Now you should be able to start up your app, but as soon as a model that includes MongoMapper::Document tries to set a callback, you'll get a nasty "undefined method before_foo" error. This happens as the MongoMapper Callbacks module is not yet compatible with the new ActiveModel API, which can be read about in a post by Yehuda Katz.

Last step. Your app should be running, but when a model is saved, you get a undefined method 'before save' kind of error. This happens as the callback module is not up-to-date with ActiveModel callbacks yet.
To get callbacks working, just drop-in-replace MongoMapper's callbacks.rb with (resides in vendor/gems/ruby..):

That's all. Nothing for production obviously, but sometimes we get impatient.

Loading mentions Retweet

Comments (2)

Feb 09, 2010
jc said...
This advise is bogus. None of these private methods that you are referring to exist anymore in the Rails3 source.
Feb 09, 2010
Kevin Valdek said...
jc, yes you're right. As you can see the post is dated almost a month ago, and a lot has happened in the Rails source since this post was published.

The above hacks are no longer necessary, MongoMapper now works out of the box with Rails 3. I'll make a remark in the beginning of the post so other readers will not fall into the same pitfall as you.

Have your say

 
Got an account with one of these? Login here, or just enter your comment below.
Posterous-login    twitter