Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoid: using more than one database?

I'm using MongoDB and MongoID in a rails app, how can some models be a part of a different mongo database on the same server? How would I accomplish something like that? I've run into the same problem with mysql before and couldn't find a reasonable solution.

Any thoughts?

like image 355
JP Silvashy Avatar asked Jan 20 '23 21:01

JP Silvashy


1 Answers

The newest versions of Mongoid support this. See the docs.

Snippets:

config/mongoid.yml:

defaults: &defaults
  host: localhost
  slaves:
    - host: localhost
      port: 27018
    - host: localhost
      port: 27019
  databases:
    secondary:
      database: secondary_database
      host: localhost
      port: 27020
      slaves:
        - host: localhost
          port: 27021
        - host: localhost
          port: 27022

In your model:

class Business
  include Mongoid::Document
  set_database :secondary
end
like image 145
PreciousBodilyFluids Avatar answered Jan 30 '23 13:01

PreciousBodilyFluids