PLEASE! how to connect to a database dynamically

I have a login dialog. It yields a user name and a password to a
controller.
Fromthere, 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

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

On 10/25/06, Horst H. [email protected] wrote:

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

Horst


Ed Hickey
Developer
Litmus Media
816-533-0409
[email protected]
A Member of Think Partnership, Inc
www.ThinkPartnership.com
Amex ticker symbol: THK

Hi Horst!

I think I would be able to help you, because I already did many
“experiments” with RoR and heterogeneous data sources, but I have
problems to figure out what you exacty would like to do. Do you have
models that are stored in different databases?

Dim

PS: Du kannst mir auch gerne eine E-Mail auf Deutsch schreiben, meine
Adresse findest Du unter www.dvisionfactory.com.

On Thursday 26 October 2006 00:26, Ed Hickey wrote:

you could do this:

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

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

On Thursday 26 October 2006 00:56, Horst H. wrote:

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

If I do this, it will work just the once.

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

Horst

On Thursday 26 October 2006 02:19, [ Dim ] wrote:

I think I would be able to help you, because I already did many
“experiments” with RoR and heterogeneous data sources, but I have
problems to figure out what you exacty would like to do. Do you have
models that are stored in different databases?

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

To me, the idea that you’ll be frequently reconnecting to your database
seems like a problem. That means that every time a user makes a request,
you’ll have to suffer through connection startup time, etc. It’s a mess.

Why not wrap these complex external databases in a separate web services
project? Then you could implement any kind of useful connection pooling
or ACL you want without trying to force your system to match the
ActiveRecord paradigm. It’ll probably be easier to maintain, too, since
it will be clear what data resides where.

Horst H. wrote:

On Thursday 26 October 2006 02:19, [ Dim ] wrote:

I think I would be able to help you, because I already did many
“experiments” with RoR and heterogeneous data sources, but I have
problems to figure out what you exacty would like to do. Do you have
models that are stored in different databases?

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

Helloo…

Is it possible to put the connection of the second database in the 

controller, this second database is dynamic which is based on the
database selected by the end user…?? I want to create an action that is
only for a database connection and i want this connection to be read or
access by all actions in a controller…how???

Thanks in advance!!!