Forum: Ruby on Rails Accessing Session Object from a model

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.
Guest (Guest)
on 2006-03-27 14:37
i realise 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

Dave
This topic is locked and can not be replied to.