Skip to content

Instantly share code, notes, and snippets.

@angelsystem
Created September 7, 2012 23:16
Show Gist options
  • Save angelsystem/3670617 to your computer and use it in GitHub Desktop.
Save angelsystem/3670617 to your computer and use it in GitHub Desktop.
Rails and MongoDB
To use multiple MongoDB database through Mongoid use set_database inside the Mongoid model to set the database
module Cache
class Inquiry
include Mongoid::Document
set_database :cache
...
And add the configuration for :cache to mongoid.yml
production:
database: abc_production
hosts: [[server1, 27017], [server2, 27017]]
databases:
cache:
database: cache_production
hosts: [[server3, 27017], [server4, 27017]]
To use namespacing, which we use for all our Mongoid Models, add the :class_name at the end of any references in the Mongoid model, for example:
module Cache
class Owner
include Mongoid::Document
embeds_one :inquiry, :class_name => 'Cache::Inquiry'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment