# Fetching Data From Mulitple Databases

**URL:** <https://www.ruby-forum.com/t/fetching-data-from-mulitple-databases/112572>\
**Category:** Rails\
**Created:** [August 21, 2007, 12:21am UTC](https://www.ruby-forum.com/t/fetching-data-from-mulitple-databases/112572 "2007-08-21T00:21:39Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mwalsh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/91b2a8/32.png) [@mwalsh](https://www.ruby-forum.com/u/mwalsh)\
**Post date:** [August 21, 2007, 12:21am UTC](https://www.ruby-forum.com/t/fetching-data-from-mulitple-databases/112572/1 "2007-08-21T00:21:39Z")

</div>

Hello,

I have read several articles on methods to fetch data from several  
databases using Rails. The general consensus seems to be that working  
with one database is better when possible, but when it’s not possible,  
you ought to establish your database connection in the model rather  
than the controller for a variety of reasons. However, just about all  
the examples I’ve found still establish the database connection in the  
controller. Can someone provide me some assistance in establishing a  
database connection in a model?

More specifically, my problem is that I am trying to store user info  
in one database and the products they own in another. I don’t get to  
choose the database design, I just get to work with what I’ve been  
given. The products database is the default database. When user  
information is requested, I need to connect to the users database and  
fetch it from there. Presently, it is done through a SOAP API to a  
third database which is really slow and problematic (the SOAP calls  
AND the database, that is - probably the initial developer’s fault).

Any suggestions would be helpful. Thanks!

---

<div class="post-metadata">

**Author:** ![mwalsh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/91b2a8/32.png) [@mwalsh](https://www.ruby-forum.com/u/mwalsh)\
**Post date:** [August 21, 2007, 4:11pm UTC](https://www.ruby-forum.com/t/fetching-data-from-mulitple-databases/112572/2 "2007-08-21T16:11:39Z")

</div>

Hi Matt, just define your connection in database.yml like the rest of  
your connections, but instead of “production”, “test”, etc name is  
something else like “usersdb”. Then in your users model, use:

establish\_connection :usersdb

Matt W. wrote:

> >

–  
Sincerely,

William P.
