# Accessing Session Object from a model

**URL:** <https://www.ruby-forum.com/t/accessing-session-object-from-a-model/56025>\
**Category:** Rails\
**Created:** [March 27, 2006, 1:28pm UTC](https://www.ruby-forum.com/t/accessing-session-object-from-a-model/56025 "2006-03-27T13:28:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![SSSSS\_SSSSSSSSSSSSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c37758/32.png) [@SSSSS\_SSSSSSSSSSSSS](https://www.ruby-forum.com/u/SSSSS_SSSSSSSSSSSSS)\
**Post date:** [March 27, 2006, 1:28pm UTC](https://www.ruby-forum.com/t/accessing-session-object-from-a-model/56025/1 "2006-03-27T13:28:32Z")

</div>

i realize that this goes against the basic idea of the MVC architecture,  
but i’ve found myself in a situation where i need to get information  
about the current user in a model & i’m not sure how to do it.

Basically, we have a product where each customer has their own profile  
with data stored in a separate database. There are some tables in a  
common ‘system-wide’ database such as the user, role & permission  
tables, but a lot of other stuff has to be divided up into separate DBs  
per customer. I realise this isn’t the best way to organise the data but  
it’s a requirement of the project so i’m stuck with it.

I need to be able to tell my model what DB to connect to based on what  
profile the logged in user has. Something similar to:

[code]  
class Report \< ActiveRecord::Base  
establish\_connection({  
:adapter =\> MyConfig.config(:db\_adaptor),  
:username =\> MyConfig.config(:db\_user\_name),  
:password =\> MyConfig.config(:db\_password),  
:database =\> @session[:user].profile.name + “\_dbname”  
})  
set\_table\_name “reports”  
.  
.  
.  
end

All the DBs have the same login information so the only thing I need to  
change is the :database attribute, but I can’t figure out any way to  
pass this information into the model before the connection takes place.  
Any help or ideas would be greatly appreciated!

Thanks in advance

Stan

---

<div class="post-metadata">

**Author:** ![SSSSS\_SSSSSSSSSSSSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c37758/32.png) [@SSSSS\_SSSSSSSSSSSSS](https://www.ruby-forum.com/u/SSSSS_SSSSSSSSSSSSS)\
**Post date:** [March 27, 2006, 2:43pm UTC](https://www.ruby-forum.com/t/accessing-session-object-from-a-model/56025/2 "2006-03-27T14:43:15Z")

</div>

ã?¹ã?¿ã?³ ã??ã?ºã?¬ã??ã?¯ wrote:

> Basically, we have a product where each customer has their own profile  
> with data stored in a separate database. There are some tables in a  
> common ‘system-wide’ database such as the user, role & permission  
> tables, but a lot of other stuff has to be divided up into separate DBs  
> per customer. I realise this isn’t the best way to organise the data but  
> it’s a requirement of the project so i’m stuck with it.

Try using a before\_filter in your controller:

before\_filter :establish\_per\_user\_db\_connection

def establish\_per\_user\_db\_connection  
Report.establish\_connection(  
:adapter =\> MyConfig.config(:db\_adaptor),  
:username =\> MyConfig.config(:db\_user\_name),  
:password =\> MyConfig.config(:db\_password),  
:database =\> @session[:user].profile.name + “\_dbname”)  
end

Zsombor

---

<div class="post-metadata">

**Author:** ![SSSSS\_SSSSSSSSSSSSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c37758/32.png) [@SSSSS\_SSSSSSSSSSSSS](https://www.ruby-forum.com/u/SSSSS_SSSSSSSSSSSSS)\
**Post date:** [March 27, 2006, 4:32pm UTC](https://www.ruby-forum.com/t/accessing-session-object-from-a-model/56025/3 "2006-03-27T16:32:19Z")

</div>

ã?¹ã?¿ã?³ ã??ã?ºã?¬ã??ã?¯ wrote:

> Well, that works but only if i want to globally switch db’s between users.  
> The thing is, i need to access a common USER table, and then switch to  
> separate dbs…

You already different connections for the User and Report models, since  
only the last was reset. How exactly does this fail?

---

<div class="post-metadata">

**Author:** ![SSSSS\_SSSSSSSSSSSSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c37758/32.png) [@SSSSS\_SSSSSSSSSSSSS](https://www.ruby-forum.com/u/SSSSS_SSSSSSSSSSSSS)\
**Post date:** [March 28, 2006, 4:32am UTC](https://www.ruby-forum.com/t/accessing-session-object-from-a-model/56025/4 "2006-03-28T04:32:01Z")

</div>

Hi. Sorry I missed out the report part of Report.establish\_connection.  
This stuff might work indeed.

I could solve my problem though using views to the user table/db from  
all my profile db’s (all the profiles have the same views to the same  
user db), so i just switch to the correct user db on arrival.

Thanks  
Stan

---

<div class="post-metadata">

**Author:** ![SSSSS\_SSSSSSSSSSSSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c37758/32.png) [@SSSSS\_SSSSSSSSSSSSS](https://www.ruby-forum.com/u/SSSSS_SSSSSSSSSSSSS)\
**Post date:** [March 27, 2006, 2:49pm UTC](https://www.ruby-forum.com/t/accessing-session-object-from-a-model/56025/5 "2006-03-27T14:49:54Z")

</div>

Well, that works but only if i want to globally switch db’s between  
users.  
The thing is, i need to access a common USER table, and then switch to  
separate dbs…

Stan
