How can I use common data and custom data in one model automatically? Common data is in CommonDB and custom data is in CustomTenantDB. My suggestion to use string ids and all data will be unique. For example: 1c - commonDB_id. 1 - customTenantDB_id. I need to set in model if I have id 1c - please use commonDB. I use two databases because I use multi tenancy and some data is shared with all tenants. I also see this link and it's almost what I need, but it doesn't describe how to do it in the model automatically. Efficient way to pull data from second database?
Asked
Active
Viewed 222 times
1 Answers
0
One thing you need to know Rails ActiveRecord creates DB connection one at a time when server starts. For multiple connection we have to override connection at model level. There are multiple implementation to do that in your case you can create Rails "callback" in each of your model and define connection hash in it with callback you can use multiple connection for models and manipulate it according to your need Or you can use gem octopus "https://github.com/thiagopradi/octopus"
There is also a good blog that can help you :
https://medium.com/@primazp/multiple-databases-in-rails-part-1-setup-48f6d207685a

Muhammad Yasir
- 91
- 1
- 5
-
Main point is: How to take data. For example i wanna take car with id 1 and 1c. My model must check both DB. Change connection automatically and provide me 2 objects. It's like split table data in two db. – Alexander Apr 25 '18 at 09:16
-
For that you have to write 2 queries before you run 1st query you need to initialize the connection object and for 2nd as well by that way you can get data from two different db by using same model But many rails geek consider this approach bad . – Muhammad Yasir Apr 25 '18 at 09:49