PLEASE! how to connect to a database dynamically

I have a login dialog. It yields a user name and a password to a controller.
From there, what is the best way (if any) to establish a database connection
using the dynamically established user name and password?

(We *have to* use the database's built in ACL features)

PLEASE help. This will make or break our decision to use RoR as a framework
for a large medical web application

Horst

I think you would need to look at this
http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M000941
it allows you to establish a connection in your controllers.

you could do this:

ActiveRecord::Base.establish_connection(
:database => “blah”,
:username => @logged_in_username,
:password => @logged_in_password,
:adapter => “mysql”,

:database => “whatever”,
:host => “localhost”
)

you might also look at the wiki: http://wiki.rubyonrails.org/rails/pages/HowtosDatabase
under the “Multiple Databases” section

If I do this, it will work just the once.
I have more than hundred tables.
I want to log in for all of them.

If I create an abstract model base class, derived from ActiveRecord::Base
and if I have the login parameters available in a *controller*, how do I pass
the parameters to that abstract base class so that all subsequent model
actions of models derived form that abstract class will sucessfully connect?

It seems so easy, and is straightforward in any other framework I know - but I
can't figure out how to do in in RoR

Horst

Ah, but if I do
ActiveRecord::Base::establish_connection(...
in my login_controller instead, it seems to work. Thanks!

Horst

1.) we have a common database (on PostgreSQL) that is accessed by multiple
programs, the RoR web app only one of many. Access control is enforced via
the backend built in ACL mechanisms - we keep it DRY and don't want to
re-implement access control in every new application accessing the database.
=> database access parameters are determined AFTER the user went through the
login screen

2.) part of our application extends two proprietary legacy databases
(Interbase/Firebird backends to proprietary frontends) which we are not
allowed to export and import into our own database

3.) there is another proprietary database (pharmaceutical reference
information) to which users can optionally subscribe - again, this one will
always reside in a separate backend.

So what I do is:
-> display login screen
-> depending on login data, connect to the main database, get authentication
parameters for third party databases from there, and connect to third party
databases as well (if applicable for that user)

This is not a traditional web app. This is replacing a complex GUI desktop
application with a browser based application

Horst

Hi Horst,

You could try doing this:

# inside login controller
@session[:main_login_params] = {:username => @params[:username],
:password => @params[:password]}
@main_db_params = {:database =>...,...}
class YourModel< ActionRecord::Base
end
YourModel.establish_connection(@main_db_params.merge(@session[:main_login_params]))
# YourModel.find etc etc.
# you could store the Legacy DB params in the @session too (may need to
find an alternative if there are a large number of users. lots of users
with complex session variables = more disk space taken up.
LegacyModel.establish_connection(@session[...])

Hope this helps.

Regards,
Simon