# Connecting to multiple databases in rails

**URL:** <https://www.ruby-forum.com/t/connecting-to-multiple-databases-in-rails/145726>\
**Category:** Rails\
**Created:** [August 13, 2008, 11:34am UTC](https://www.ruby-forum.com/t/connecting-to-multiple-databases-in-rails/145726 "2008-08-13T11:34:31Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [August 13, 2008, 11:34am UTC](https://www.ruby-forum.com/t/connecting-to-multiple-databases-in-rails/145726/1 "2008-08-13T11:34:31Z")

</div>

Hi  
When strting my application I have three text boxes Username,  
password and company name  
A user on providing all these goes to user controller login\_process  
action…That means in my application database.yml have no relevance .It  
is as  
def login\_process  
ActiveRecord::Base.establish\_connection(  
:adapter =\> ‘postgresql’,  
:host =\> ‘192.168.1.9’,  
:username =\> ‘postgres’,  
:password =\> ‘password’,  
:database =\> ‘gatekeeper’  
)  
@the\_site = Site.find\_by\_site\_code(params[:company][:whichCompany])

#This sites table contains information of to which different databases  
to connect,the adapter using,IPaddress of database server etc based on  
company

ActiveRecord::Base.establish\_connection(  
:adapter =\> @the\_site.db\_adapter,  
:host =\> @the\_site.db\_host,  
:username =\> @the\_site.db\_user,  
:password =\> @the\_site.db\_pw,  
:database =\> @the\_site.db\_name  
)  
-----code continues  
end  
So what I am doing is suppose a user say user1 with password pass1  
and gives company1 then it first connects to gatekeeper database and  
from that based on site code gets adapter,host,db\_user,db\_pw and db\_name  
information and reconnected to that db as above…And I could  
successfully do it  
Now my problem is suppose a second user say user2 with passord  
pass2 and company company2 connects to the server what happens is now  
the already logined first user’s (user1) database connection information  
changes to that of second That is the last login users information…  
I think I can make you understand the problem…Please help…Why  
this happens…How to solve this?

Thanks in advance  
Sijo

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [August 13, 2008, 12:44pm UTC](https://www.ruby-forum.com/t/connecting-to-multiple-databases-in-rails/145726/2 "2008-08-13T12:44:03Z")

</div>

Create a seperate rails application for handling the login /  
gatekeeper interaction. On successful login, pass the details of the  
database connection to another rails application and store these  
values in the session of the second application and use it to  
establish connections.

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [August 13, 2008, 12:49pm UTC](https://www.ruby-forum.com/t/connecting-to-multiple-databases-in-rails/145726/3 "2008-08-13T12:49:19Z")

</div>

For security, get the session id of the user upon successful login  
from the first application and pass that on to the secondary  
application. You could use a table in the gatekeeper database to  
store the session information without worrying about cookies. This  
needs more analysis though.

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [August 13, 2008, 12:57pm UTC](https://www.ruby-forum.com/t/connecting-to-multiple-databases-in-rails/145726/4 "2008-08-13T12:57:59Z")

</div>

Hi  
Thanks for the reply…Just to ask is this the only solution  
available?  
Sijo

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [August 13, 2008, 3:03pm UTC](https://www.ruby-forum.com/t/connecting-to-multiple-databases-in-rails/145726/5 "2008-08-13T15:03:34Z")

</div>

You could maintain a lookup hash in your session with the user\_id and  
the db\_connect parameters and do a connect / disconnect before a DB  
operation which is a nightmare for performance. I will be interested  
to know about alternative approaches.

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [August 13, 2008, 7:28pm UTC](https://www.ruby-forum.com/t/connecting-to-multiple-databases-in-rails/145726/6 "2008-08-13T19:28:49Z")

</div>

Hi Sijo,

> Thanks for the reply…Just to ask is this the only solution  
> available?

You could look into Dr. Nic’s Magic Multi-Connections [1] or setup  
multiple database connections in config/database.yml. From the top of my  
head I don’t know how to use the latter, but you could look into how  
Mephisto [2] does it.

[1] [http://magicmodels.rubyforge.org/magic\_multi\_connections/](http://magicmodels.rubyforge.org/magic_multi_connections/)  
[2] [http://mephistoblog.com/](http://mephistoblog.com/)

–  
Roderick van Domburg

> **[De domeinnaam Nedforce.nl is te koop.](http://www.nedforce.nl)**
>
> Deze mooie domeinnaam kan al over een paar minuten van jou zijn. Betalen gaat eenvoudig en vertrouwd via iDEAL. Direct na de betaling ontvang je de verhuiscode per e-mail en kun je aan de slag.
