Forum: Ruby on Rails Multiple database login issue

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
shaan s. (Guest)
on 2009-03-09 18:03
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.
Frederick C. (Guest)
on 2009-03-09 18:48
(Received via mailing list)
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
shaan s. (Guest)
on 2009-03-09 19:54
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?
Frederick C. (Guest)
on 2009-03-09 21:18
(Received via mailing list)
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
shaan s. (Guest)
on 2009-03-11 10:11
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/",

I have no idea why this happens, may be someone can shed some light.
This topic is locked and can not be replied to.