Forked from hwatkins/mongoid_3_multiple_database.txt
Created
November 5, 2019 05:54
-
-
Save bootjp/4525317a403e140d4e5a4495fe932c10 to your computer and use it in GitHub Desktop.
Mongoid 3.x multiple database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
If you want to use multiple dbs at once there are several different ways... | |
1) If you want to do this on a per-model level, use .store_in (This is for all threads): | |
class Band | |
include Mongoid::Document | |
store_in database: "secondary" # This can be any name you want, no need to put it in the mongoid.yml. | |
end | |
class Artist | |
include Mongoid::Document | |
store_in database: "tertiary" | |
end | |
2) If you want to do this on a per-operation level, use #with (This is for current thread only): | |
Band.with(database: "secondary").create(...)! | |
band.with(database: "tertiary").update_attributes(...) | |
3) If you want to override this on a global level, like per-request switching, use .override_database (This is for current thread only): | |
Mongoid.override_database("secondary") | |
Band.create # Persists to secondary | |
band.save # Persists to secondary | |
I am assuming you mean in this case multiple databases in the same server or replica set... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment