# How to manage multiple databases?

**URL:** <https://www.ruby-forum.com/t/how-to-manage-multiple-databases/134594>\
**Category:** Rails\
**Created:** [April 12, 2008, 5:20pm UTC](https://www.ruby-forum.com/t/how-to-manage-multiple-databases/134594 "2008-04-12T17:20:26Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![explainer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/bbce88/32.png) [@explainer](https://www.ruby-forum.com/u/explainer)\
**Post date:** [April 12, 2008, 5:20pm UTC](https://www.ruby-forum.com/t/how-to-manage-multiple-databases/134594/1 "2008-04-12T17:20:26Z")

</div>

I am working with a customer who wants a web application built that  
must access a remote MS SQL-Server database, while also accessing a  
local MySQL db. Is there any best practice about how to construct  
this? The plan is for the MySQL db to be the rails host db, with the  
SQL-Server db containing tables of existing information.

Do I have to build custom code to access one of the databases?

This must be a common occurrence, so it must have been addressed  
previously. I do not have the need to coordinate both databases  
within the same transaction.

Any suggestions are welcome.

---

<div class="post-metadata">

**Author:** ![explainer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/bbce88/32.png) [@explainer](https://www.ruby-forum.com/u/explainer)\
**Post date:** [April 14, 2008, 4:25am UTC](https://www.ruby-forum.com/t/how-to-manage-multiple-databases/134594/2 "2008-04-14T04:25:16Z")

</div>

The basic idea is to create a new class from which your MS\*SQL models  
can inherit their connection information. You have to declare that  
the class is abstract so that Rails will not assume you’re using STI.  
It would look something like this:

class MsSqlConnection \< ActiveRecord::Base  
self.abstract\_class = true  
establish\_connection “#{RAILS\_ENV}\_mssql”.to\_sym  
end

With a class like that you can have database.yml with a development,  
test, and production section describing mysql and development\_mssql,  
test\_mssql, and production\_mssql describing how to access your remote  
MS\*SQL db.

Finally, the models that map to the mssql db inherit from  
MsSqlConnection rather than ActiveRecord::Base.
