gberz3
2009-Jun-08 19:03 UTC
Dynamically connecting a model to databases in a running app…?
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.
Frederick Cheung
2009-Jun-08 21:36 UTC
Re: Dynamically connecting a model to databases in a running app…?
On Jun 8, 8:03 pm, gberz3 <gbe...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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. >Well you can can establish_connection pretty much whenever you want. Have you tried that ? Fred> 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.
gberz3
2009-Jun-08 23:06 UTC
Re: Dynamically connecting a model to databases in a running app…?
On Jun 8, 5:36 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Well you can can establish_connection pretty much whenever you want. > Have you tried that ? > > FredSo 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.