# Best solution for accessing multiple databases?

**URL:** <https://www.ruby-forum.com/t/best-solution-for-accessing-multiple-databases/117415>\
**Category:** Rails\
**Created:** [October 16, 2007, 5:51pm UTC](https://www.ruby-forum.com/t/best-solution-for-accessing-multiple-databases/117415 "2007-10-16T17:51:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@matt](https://www.ruby-forum.com/u/matt)\
**Post date:** [October 16, 2007, 5:51pm UTC](https://www.ruby-forum.com/t/best-solution-for-accessing-multiple-databases/117415/1 "2007-10-16T17:51:14Z")

</div>

Good morning,

I’m currently running my Rails app out of PostgreSQL database but I’ve  
gotten to a point where I need to query another Oracle database to  
retrieve a single record.

I’ve seen approaches like this:  
[http://wiki.rubyonrails.org/rails/pages/HowtoUseMultipleDatabases](http://wiki.rubyonrails.org/rails/pages/HowtoUseMultipleDatabases)

But is this the right method for me?

When a user uses my application it determines who they are via their  
Windows Login. Using that login I have the capability to query an  
Oracle database and retrieve all the information on that user. So when  
the user logs into my application I would like to make the call to that  
Oracle database, pull down their information and continue with the  
application never having to access the Oracle database again.

Are there any other known methods out there that work very well?

---

<div class="post-metadata">

**Author:** ![matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@matt](https://www.ruby-forum.com/u/matt)\
**Post date:** [October 16, 2007, 6:09pm UTC](https://www.ruby-forum.com/t/best-solution-for-accessing-multiple-databases/117415/2 "2007-10-16T18:09:03Z")

</div>

> Windows Login. Using that login I have the capability to query an  
> Oracle database and retrieve all the information on that user. So when  
> the user logs into my application I would like to make the call to that  
> Oracle database, pull down their information and continue with the  
> application never having to access the Oracle database again.

Why wouldn’t you setup the oracle database in database.yml then create a  
model like ‘AllUserInfo’ or something that makes more sense and define  
it  
like this:

class AllUserInfo \< ActiveRecord::Base  
establish\_connection ‘oracle\_db\_defined\_in\_yml\_file’  
set\_table\_name ‘whatever my table name is’  
end

and then just do…

all\_info\_about\_a\_user = AllUserInfo.find(:first, :conditions…)

?

---

<div class="post-metadata">

**Author:** ![matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@matt](https://www.ruby-forum.com/u/matt)\
**Post date:** [October 16, 2007, 6:18pm UTC](https://www.ruby-forum.com/t/best-solution-for-accessing-multiple-databases/117415/3 "2007-10-16T18:18:59Z")

</div>

On 10/16/07, Matthew W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> But is this the right method for me?
> 
> When a user uses my application it determines who they are via their  
> Windows Login. Using that login I have the capability to query an  
> Oracle database and retrieve all the information on that user. So when  
> the user logs into my application I would like to make the call to that  
> Oracle database, pull down their information and continue with the  
> application never having to access the Oracle database again.

Is there one Oracle DB or are there several and which one needs to be  
determined based on the user or some other criteria? The article you  
link to talks about the latter.

It it’s just one DB and you have one or more tables to be ‘statically’  
connected to that DB , then see  
[Peak Obsession](http://rails.rubyonrails.com/classes/ActiveRecord/Base.html) and look  
for the section “Connection to multiple databases in different models”

You probably want to call establish\_connection with the right  
connection parameters for the table(s) to be connected to the oracle  
DB in your config/environment.rb file in a config.after\_initialize  
block. If you’ve got several tables you can make an abstract AR class  
for those models to subclass, and establish the connection for the  
abstract class which will take care of the subclass.

I’m working on a project right now which does something similar. One  
thing I’ve found is that most of the testing infrastructure of rails  
doesn’t really handle tables with their own connections very well,  
what I’ve done is to put analogs of those tables directly in the main  
test environment database. I wrote the migrations for the ‘external’  
tables so that they only actually create the tables if  
ENV[‘RAILS\_ENV’] is ‘test’

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@matt](https://www.ruby-forum.com/u/matt)\
**Post date:** [October 16, 2007, 7:20pm UTC](https://www.ruby-forum.com/t/best-solution-for-accessing-multiple-databases/117415/4 "2007-10-16T19:20:09Z")

</div>

Philip H. wrote:

> > Windows Login. Using that login I have the capability to query an  
> > Oracle database and retrieve all the information on that user. So when  
> > the user logs into my application I would like to make the call to that  
> > Oracle database, pull down their information and continue with the  
> > application never having to access the Oracle database again.
> 
> Why wouldn’t you setup the oracle database in database.yml then create a  
> model like ‘AllUserInfo’ or something that makes more sense and define  
> it  
> like this:
> 
> class AllUserInfo \< ActiveRecord::Base  
> establish\_connection ‘oracle\_db\_defined\_in\_yml\_file’  
> set\_table\_name ‘whatever my table name is’  
> end
> 
> and then just do…
> 
> all\_info\_about\_a\_user = AllUserInfo.find(:first, :conditions…)
> 
> ?

I was unaware of this capability but it seems like the best bet.

That’s all I really need to do, it’s a single Oracle database with just  
one lookup table that I need to access.

Thanks for the tip, I’ll give it a shot.
