Ruby gem or snippet to use different ActiveRecord db connection depending on model attribute value

82 Views Asked by At

Can you suggest a ruby gem or show a snippet with exsmple how to make AtiveRecord::Base successor to choose a db connection depending on a model record id?

1

There are 1 best solutions below

3
On

Ok. Please let me know if the following solution can work if not then I will delete it from here.

I wouldn't use Gem for this.

Here a mud map how I would tackle this project. ( for small to medium projects only )

enter image description here

  • Ruby Class: manages all the connections.
  • Query handler: dictates what connection do we need based on the condition.
  • Connector is a simple class that grabs stuff from Rails and trigger the query handlers.

Query handler is the optional bit. You can connect your Rails directly to the Ruby Class. ( I don't like this as they will be coupled a bit too much )

Have your database.yaml file (just to keep Rails happy) Your application will be using the Ruby Class connector and query handler to grab stuff from different databases, No Rails needed there.

Hope this help!

PS: Again if you think this is not good solution let me know and I see what I can do. Cheers mate.