Monday, July 16, 2012

Using Mongoid 3 (MongoHQ or MongoLab) on Heroku

Here's what you need to get Mongoid working on Heroku, if you're getting errors like:
NoMethodError (undefined method `[]' for nil:NilClass)

Make sure the following is in your gem file (reference).
source 'http://rubygems.org' 
ruby '1.9.3'
gem  'rails', '3.2.3'

If you get an error like undefined method `ruby' run the following:
gem install bundler --pre

A lot of places have outdate documentation for configuring your mongoid.yml file which may result in the following error:
No database provided for session configuration: :default

Your mongoid.yml file should look like the following.
production:
  sessions:
    default:
      uri: <%= ENV['MONGOHQ_URL'] %>
      options:
        skip_version_check: true
        safe: true
The ENV variable above is for MongoHQ. For MongoLab use ENV['MONGOLAB_URI'] 


Big thanks to the guys in this StackOverflow thread for helping me put all this together!