# Rails App Database in MySQL - OK, However what if I need to access multiple Databases of different

**URL:** <https://www.ruby-forum.com/t/rails-app-database-in-mysql-ok-however-what-if-i-need-to-access-multiple-databases-of-different/141478>\
**Category:** Rails\
**Created:** [June 26, 2008, 9:36am UTC](https://www.ruby-forum.com/t/rails-app-database-in-mysql-ok-however-what-if-i-need-to-access-multiple-databases-of-different/141478 "2008-06-26T09:36:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![housekeeping](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/838e76/32.png) [@housekeeping](https://www.ruby-forum.com/u/housekeeping)\
**Post date:** [June 26, 2008, 9:36am UTC](https://www.ruby-forum.com/t/rails-app-database-in-mysql-ok-however-what-if-i-need-to-access-multiple-databases-of-different/141478/1 "2008-06-26T09:36:33Z")

</div>

Hi,

Although I am an experienced developer, I am new to rails and enjoying  
how easy it is to build applications.  
I seem to have come across a stumbling block and was wondering if  
anyone out there has come across the same problem and could suggest  
possible solutions.

Problem:

I have an intranet-based application in a company network - using  
rails. The application has many sub-applications which may be composed  
of a handful of pages each.

The web application has its own database using MySQL which contains  
all application specific data, however, I need to retrieve data from  
different sources which maybe, MSSQL, Oracle or Sybase databases.

For example, I need to access a well establish Sybase database server  
which contains trade data so I can create a summary of daily Profit &  
Loss - after filtering, various calculations I will archive this  
summary data in the Applications MySQL DB for performance views  
etc.over time.

The Question:

In rails how do I do this neatly, rails is designed to use one  
database right?  
Would anyone be able to suggest ways in which I can access such data?

Many thanks in advance.

H

---

<div class="post-metadata">

**Author:** ![housekeeping](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/838e76/32.png) [@housekeeping](https://www.ruby-forum.com/u/housekeeping)\
**Post date:** [June 26, 2008, 4:37pm UTC](https://www.ruby-forum.com/t/rails-app-database-in-mysql-ok-however-what-if-i-need-to-access-multiple-databases-of-different/141478/2 "2008-06-26T16:37:52Z")

</div>

You’ll need to take over the connection for your different models (this  
model is MySQL, that model is Sybase, etc).

Start here, and google for other examples:

[http://api.rubyonrails.com/classes/ActiveRecord/Base.html](http://api.rubyonrails.com/classes/ActiveRecord/Base.html)

(I’m currently trying to avoid this issue myself in a company app, but I  
don’t think it will go away, and I won’t be able to avoid it for much  
longer - I’ll have to connect to MySQL, SQL Server, and Oracle - sigh)

---

<div class="post-metadata">

**Author:** ![housekeeping](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/838e76/32.png) [@housekeeping](https://www.ruby-forum.com/u/housekeeping)\
**Post date:** [June 26, 2008, 6:26pm UTC](https://www.ruby-forum.com/t/rails-app-database-in-mysql-ok-however-what-if-i-need-to-access-multiple-databases-of-different/141478/3 "2008-06-26T18:26:22Z")

</div>

> For example, I need to access a well establish Sybase database server  
> which contains trade data so I can create a summary of daily Profit &  
> Loss - after filtering, various calculations I will archive this  
> summary data in the Applications MySQL DB for performance views  
> etc.over time.
> 
> The Question:
> 
> In rails how do I do this neatly, rails is designed to use one  
> database right?  
> Would anyone be able to suggest ways in which I can access such data?

Set up the database connection in database.yml. Let’s say it’s named  
“sybasefu”.

In the models that need to access that database do this:

class SomeThing \< ActiveRecord::Base  
establish\_connection “sybasefu”  
#…  
end

One thing to check is if you have several models that need to connect  
to sybasefu, _each_ one will open it’s own connection. Unlike your  
“normal” models which share a single database connection. This was  
true for Rails 1.1.6 (and yes, I realize how old that is :). To get  
around it I did this:

class MySybaseClasses \< ActiveRecord::Base  
establish\_connection “sybasefu”  
end

class Something \< MySpaceClasses  
#…  
end

class SomethingElse \< MySpaceClasses  
#…  
end

That may or may not still be an issue, but it’s something to check.  
If it isn’t, let me know 🙂

-philip

---

<div class="post-metadata">

**Author:** ![housekeeping](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/838e76/32.png) [@housekeeping](https://www.ruby-forum.com/u/housekeeping)\
**Post date:** [June 27, 2008, 3:25am UTC](https://www.ruby-forum.com/t/rails-app-database-in-mysql-ok-however-what-if-i-need-to-access-multiple-databases-of-different/141478/4 "2008-06-27T03:25:04Z")

</div>

Thanks Phillip, checking it out right now… this looks so obvious!!  
Will let you know how it all pans out, but I am thinking this should  
do the trick in combination with find\_by\_sql() for the query.

Very helpful thank you 🙂
