# ActiveRecord::Base.establish\_connection

**URL:** <https://www.ruby-forum.com/t/activerecord-base-establish-connection/145554>\
**Category:** Rails\
**Created:** [August 11, 2008, 2:56pm UTC](https://www.ruby-forum.com/t/activerecord-base-establish-connection/145554 "2008-08-11T14:56:38Z")\
**Posts on this page:** 2\
**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 11, 2008, 2:56pm UTC](https://www.ruby-forum.com/t/activerecord-base-establish-connection/145554/1 "2008-08-11T14:56:38Z")

</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(for example say appl\_db on host  
192.168.1.45) 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(surely this user has a  
sepearate db since he belongs to a differnt company say for example  
appl\_db2 on some other host) 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 12, 2008, 9:03am UTC](https://www.ruby-forum.com/t/activerecord-base-establish-connection/145554/2 "2008-08-12T09:03:25Z")

</div>

Hi  
I could not solve this problem yet…Please help  
Sijo
