# Dynamically connecting a model to databases in a running appâ€¦?

**URL:** <https://www.ruby-forum.com/t/dynamically-connecting-a-model-to-databases-in-a-running-appa/169793>\
**Category:** Rails\
**Created:** [June 8, 2009, 9:04pm UTC](https://www.ruby-forum.com/t/dynamically-connecting-a-model-to-databases-in-a-running-appa/169793 "2009-06-08T21:04:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dezrtdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f4b2a3/32.png) [@dezrtdude](https://www.ruby-forum.com/u/dezrtdude)\
**Post date:** [June 8, 2009, 9:04pm UTC](https://www.ruby-forum.com/t/dynamically-connecting-a-model-to-databases-in-a-running-appa/169793/1 "2009-06-08T21:04:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dezrtdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f4b2a3/32.png) [@dezrtdude](https://www.ruby-forum.com/u/dezrtdude)\
**Post date:** [June 8, 2009, 11:37pm UTC](https://www.ruby-forum.com/t/dynamically-connecting-a-model-to-databases-in-a-running-appa/169793/2 "2009-06-08T23:37:22Z")

</div>

On Jun 8, 8:03 pm, gberz3 [removed\_email\_address@domain.invalid](mailto: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

---

<div class="post-metadata">

**Author:** ![dezrtdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f4b2a3/32.png) [@dezrtdude](https://www.ruby-forum.com/u/dezrtdude)\
**Post date:** [June 9, 2009, 1:06am UTC](https://www.ruby-forum.com/t/dynamically-connecting-a-model-to-databases-in-a-running-appa/169793/3 "2009-06-09T01:06:54Z")

</div>

On Jun 8, 5:36 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto: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.
