Using Mongoid on Heroku with MongoHQ
Heroku just announced a new MongoHQ addon which is totally awesome. MongoHQ + Heroku is by far the fastest way to get a MongoDB backed application up and running on the web (for free non the less).
Heroku’s docs have instructions for setting up the addon with MongoMapper but not with Mongoid (my current Mongo ORM of choice). I didn’t see an easy way to configure Mongoid from a url, so I added this to my application.rb file before the require ‘rails/all’ line. (For rails 2.x users, you can put this in a preninitializer.rb file in your config directory. This works with the default mongoid.yml config file generated by Mongoid.
require ‘uri’
if ENV[“MONGOHQ_URL”]
mongo_uri = URI.parse(ENV[“MONGOHQ_URL”])
ENV[‘MONGOID_HOST’] = mongo_uri.host
ENV[‘MONGOID_PORT’] = mongo_uri.port.to_s
ENV[“MONGOID_USERNAME”] = mongo_uri.user
ENV[‘MONGOID_PASSWORD’] = mongo_uri.password
ENV[‘MONGOID_DATABASE’] = mongo_uri.path.gsub(“/”, “”)
end
Maybe configuration via url will get added to the Mongoid Gem or the addon will support config via these variables but until then this is a nice and quick solution to get up and running.
3 months ago