Re: multiple database users for rails app?

Ball, Donald A Jr \(Library\)
Tue, 23 Jan 2007 18:05:20 -0000

> Sounds to me like you want two DB connections, one for each
> user.  Use the session variable to choose which connection to
> use.  Multiple connections to one db should be equivalent to
> connecting to multiple
> databases: http://www.google.com/search?q=rails+multiple+databases

Yes, that''s what I want to do, but this doesn''t seem to be a
common
practice, or at least a well-documented common practice. I''ll see what
I
can do to help fix the latter. :)

It would seem the pragmatic thing to do would be to define a parallel
set of database targets in database.yml (admin_development,
admin_production, etc.) and in the admin controllers, using a
before_filter, call ActiveRecord::Base.establish_connection with the
admin users'' credentials. Three questions come up:

1. Can I access the rails Configuration object at request-time? I mean,
I could parse database.yml manually, but that''s redundant. Alternately,
I could store a reference to the Configuration object in environment.rb.
Any suggestions?

2. If I manually call ActiveRecord::Base.establish_connection, should I
close the original connection first? Should I close the newly created
connection myself in an after_filter? What should I do in case of
exceptions?

3. Do the answers vary depending on if I running in a FastCGI, CGI, or
Mongrel environment?

Any tips are greatly appreciated. Any pointers towards comprehensive
documentation of the rails'' application and requests lifecycles would
be
helpful too. Cheers.

- doald