Dynamically connecting a model to databases in a running app…?


#1

Hi All,

I’ve read many of the existing questions/threads on this subject, but
keep in mind that none of them have directly addressed my issue – or
they have yet to be answered. Also keep in mind that this is NOT a
situation for database.yml as I won’t know the DB info in advance.

That said, I need a solution for DYNAMICALLY connecting to multiple
databases in a Rails app. My situation is that I have multiple data
logging sites, all with a simple data table (EVENTS, TIMESTAMP,
VALUE). These sites need to (and will) remain as they are due to local
applications that need to use them.

What I need is to create an application that maintains a table of
“MYSQL_LOGINS” and uses each of those logins to connect to any one of
various databases and provide charts/graphs for the data therein.

My “data” model will remain the same for all MySQL databases to which
I connect, I simply need to be able to tell my MODEL to connect to a
different DB on the fly. I’m hoping this is amazingly simple, but
we’ll see.

Best.


#2

On Jun 8, 8:03 pm, gberz3 removed_email_address@domain.invalid wrote:

VALUE). These sites need to (and will) remain as they are due to local
applications that need to use them.

Well you can can establish_connection pretty much whenever you want.
Have you tried that ?

Fred


#3

On Jun 8, 5:36 pm, Frederick C. removed_email_address@domain.invalid
wrote:

Well you can can establish_connection pretty much whenever you want.
Have you tried that ?

Fred

So you’re suggesting I call it on the model like
“User.establish_connection” as needed? I wasn’t aware that was
something reasonable/feasible. I’ll give it a try.