# Multiple database login issue

**URL:** <https://www.ruby-forum.com/t/multiple-database-login-issue/162405>\
**Category:** Rails\
**Created:** [March 9, 2009, 5:03pm UTC](https://www.ruby-forum.com/t/multiple-database-login-issue/162405 "2009-03-09T17:03:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![shanker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90db22/32.png) [@shanker](https://www.ruby-forum.com/u/shanker)\
**Post date:** [March 9, 2009, 5:03pm UTC](https://www.ruby-forum.com/t/multiple-database-login-issue/162405/1 "2009-03-09T17:03:01Z")

</div>

Here is a problem which is quite mysterious to me,

I am having multiple database architecture, each user logs in with  
organisation name/user name/password  
There is a master database and a table ‘names’ which contains the names  
of the database corresponding to each organisation name

I am using  
ActiveRecord::Base.connection.execute(‘USE ‘+name\_from  
\_master\_db\_table+’’)

to dynamically switch the database, also i am using act\_as\_authenticated  
plugin to login the users

Now the issue is that i need to login twice to enter, also if i login in  
a different browser with a different org name(which connects to a  
different db ) the page in the initial browser gets logged out, All  
these used to work fine when the session store was cookie based , now  
that i have changed to active\_record store, I am facing these issues,  
any idea or suggestion on this would be helpful a lot.

---

<div class="post-metadata">

**Author:** ![shanker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90db22/32.png) [@shanker](https://www.ruby-forum.com/u/shanker)\
**Post date:** [March 9, 2009, 5:48pm UTC](https://www.ruby-forum.com/t/multiple-database-login-issue/162405/2 "2009-03-09T17:48:41Z")

</div>

On 9 Mar 2009, at 16:03, Shank Shaan wrote:

> ActiveRecord::Base.connection.execute('USE '+name\_from  
> these used to work fine when the session store was cookie based , now  
> that i have changed to active\_record store, I am facing these issues,  
> any idea or suggestion on this would be helpful a lot.

well when you switch the database like that you’re probably leaving it  
switched, so when the next request is handled it’s trying to get the  
session from the wrong database.

Fred

---

<div class="post-metadata">

**Author:** ![shanker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90db22/32.png) [@shanker](https://www.ruby-forum.com/u/shanker)\
**Post date:** [March 9, 2009, 6:54pm UTC](https://www.ruby-forum.com/t/multiple-database-login-issue/162405/3 "2009-03-09T18:54:22Z")

</div>

Frederick C. wrote:

> On 9 Mar 2009, at 16:03, Shank Shaan wrote:
> 
> > ActiveRecord::Base.connection.execute('USE '+name\_from  
> > these used to work fine when the session store was cookie based , now  
> > that i have changed to active\_record store, I am facing these issues,  
> > any idea or suggestion on this would be helpful a lot.
> 
> well when you switch the database like that you’re probably leaving it  
> switched, so when the next request is handled it’s trying to get the  
> session from the wrong database.
> 
> Fred

Thanks fred, but I am storing the db info in the cookie and using a  
before filter in the application.rb, so I guess its some issue with  
act\_as\_auth plugin?

---

<div class="post-metadata">

**Author:** ![shanker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90db22/32.png) [@shanker](https://www.ruby-forum.com/u/shanker)\
**Post date:** [March 11, 2009, 9:11am UTC](https://www.ruby-forum.com/t/multiple-database-login-issue/162405/4 "2009-03-11T09:11:36Z")

</div>

Thanks so much fred for the reply, posted this question in many forums  
but unfotunately, did not recieve even a single reply except yours.  
thanks again.

I could not figure out why this issue appears, i donot thing the  
before\_filter from acts\_as\_auth runs before the before\_filter that sets  
the db

this is the order in application.rb

before\_filter :connection  
include AuthenticatedSystem  
before\_filter :login\_from\_cookie,:login\_required

Struggling with this issue for over 2 weeks now I have temporarily  
fixed this issue by making the sessions store in a single database for  
all organisations by using  
CGI::Session::ActiveRecordStore::Session.establish\_connection, so not it  
works fine with multiple db architecture.

Meanwhile one thing that I noticed was the session id in the request.env  
was different from the one that shows up in the log

Processing HomeController#index (for 127.0.0.1 at 2009-03-11 13:02:07)  
[GET]  
Session ID: 97dfbf70c7839f876927fdb9f69ffe37

“HTTP\_COOKIE”=\>“\_session\_id=7e4a42b0acc8a333c2dc00fd64a644cb;  
dbname=me\_dev”, “HTTP\_ACCEPT\_CHARSET”=\>“ISO-8859-1,utf-8;q=0.7,\*;q=0.7”,  
“REQUEST\_URI”=\>“[http://localhost:3000/](http://localhost:3000/)”,

I have no idea why this happens, may be someone can shed some light.

---

<div class="post-metadata">

**Author:** ![shanker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90db22/32.png) [@shanker](https://www.ruby-forum.com/u/shanker)\
**Post date:** [March 9, 2009, 8:18pm UTC](https://www.ruby-forum.com/t/multiple-database-login-issue/162405/5 "2009-03-09T20:18:04Z")

</div>

On 9 Mar 2009, at 17:54, shaan ss wrote:

> > well when you switch the database like that you’re probably leaving  
> > it  
> > switched, so when the next request is handled it’s trying to get the  
> > session from the wrong database.
> > 
> > Fred
> 
> Thanks fred, but I am storing the db info in the cookie and using a  
> before filter in the application.rb, so I guess its some issue with  
> act\_as\_auth plugin?

The fact that it worked fine when the session store was cookie based  
makes that seem unlikely. Perhaps the before\_filter from acts\_as\_auth  
runs before your before\_filter that sets the db.

Fred
