Same model connecting to different databases

I need to build a dashboard app that will show data from multiple Rails apps which have their own database. So I have a Project model in each Rails app but in my dashboard I need to show consolidated data from Project model from each of the Rails apps (e,g, status of projects in each of the apps). Controllers for each of the Rails apps are protected by authentication and authorization. What is the best way to do that? Should I expose a new controller in each app and make active resource calls to each one of them or is there a simple way in my Dashboard app to connect to each Rails app database Project model and aggregate project data?

Instead of exposing a new controller per app, why not create different model objects for each of the different Rails apps you’re trying to see? It seems to me that the different database connections should be the concern of the model, not the concern of the controller.

You can add new connections to your database.yml and have each of these models connect to the different database connections.

So are you suggesting a new model for each customer project (e.g. Cust1Project, Cust2Project etc.) in dashboard app? And each of these models can make a AR connection to their specific db, right?

I would set it up something like that. And yes, each of these models would make an AR connection to their specific DB.

Essentially, your set up would be done this way:


class CustomerOneProject < ActiveRecord::Base 
  establish_connection :customer_one_project

class CustomerTwoProject < ActiveRecord::Base 
  establish_connection :customer_two_project

…And so on.

I’d be a bit careful going that root - connecting multiple db’s can often introduce a lot of unforseen issues down the road namely security and performance. If you feel that you have both of those nailed, possibly but I’ve always tried to steer away from that. Sounds like you want a set of microservices on top of these external databases.

Agreed on your point, actually. I’m not a huge fan of connecting to multiple DBs in one app. However, if for whatever reason you can’t set up microservices, then that setup might be the (rather unfortunate) way to go.