0

I'm fairly new to Ruby, and have only started using Rails within the last week. I need to connect to several databases that have identical table structure. We use several different instances of Crucible, so our data is all over the place. Here is an easy example of what I mean. The tables are absolutely identical, save for the data inside of them. How could I go about accessing these tables without creating new models for each database I need to connect to?

  • you need to switch dbs sometimes or access both of them concurrently? see e.g. http://www.thegreatcodeadventure.com/managing-multiple-databases-in-a-single-rails-application/ – mb21 Oct 17 '17 at 19:39
  • @mb21 I had seen at that article earlier before posting this question, but mostly skimmed through it. MOST of the time, they would be accessed concurrently. If I want to get users, I would like all users across all db instances. –  Oct 17 '17 at 19:42
  • my guess is that you'd have to hack into active record. that's definitely not a usecase default rails was designed to solve. Sounds also pretty inefficient, as you'll have to sort, order etc. all the results again after you aggregate them from the different databases. Or are you saying it's on the same database server (same connection), simply in a different database? – mb21 Oct 18 '17 at 09:42
  • @mb21 They're all on the same host, just different databases. –  Oct 18 '17 at 13:31
  • Then you should be able to do a SQL UNION, which active record still doesn't support AFAIK... see e.g. https://coderwall.com/p/9hohaa/activerecord-union-hack for a workaround, but maybe you'll have to look for an ORM that supports UNION or write [raw sql](https://stackoverflow.com/questions/6686920/activerecord-query-union). – mb21 Oct 18 '17 at 13:55

1 Answers1

0

You need to use second base gem for managing to different databases. Here is the link

Aniket Tiwari
  • 3,561
  • 4
  • 21
  • 61
  • This is close to what I'm looking for, but it seems that secondbase only allows for one other configuration. I need to allow for six. –  Oct 18 '17 at 14:52