Forum: Ruby on Rails Dynamically connecting a model to databases in a running app…?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
A587929f80f37c163f855f5610f2aa0c?d=identicon&s=25 Michael Williams (gberz3)
on 2009-06-08 21:04
(Received via mailing list)
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.
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-06-08 23:37
(Received via mailing list)
On Jun 8, 8:03 pm, gberz3 <gbe...@gmail.com> 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
A587929f80f37c163f855f5610f2aa0c?d=identicon&s=25 Michael Williams (gberz3)
on 2009-06-09 01:06
(Received via mailing list)
On Jun 8, 5:36 pm, Frederick Cheung <frederick.che...@gmail.com>
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.
This topic is locked and can not be replied to.