# Acces multiply databases?

**URL:** <https://www.ruby-forum.com/t/acces-multiply-databases/130838>\
**Category:** Rails\
**Created:** [March 3, 2008, 8:17pm UTC](https://www.ruby-forum.com/t/acces-multiply-databases/130838 "2008-03-03T20:17:14Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![remco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9d8465/32.png) [@remco](https://www.ruby-forum.com/u/remco)\
**Post date:** [March 3, 2008, 8:17pm UTC](https://www.ruby-forum.com/t/acces-multiply-databases/130838/1 "2008-03-03T20:17:14Z")

</div>

Hi,

i build my own rails-app, with “my” (own)database for a new website. My  
company has a big central database (also mysql\> same server \> cluster \>  
but no rails-app) with some interesting content i want to use…customer  
reviews, faq, country/city-descriptions ect.

How can i use this data in my app?

Some requirements

I want to load the cities-data from the central database in my rails-app  
and then use Acts As Taggable to do some tagging on the cities.

Grtz

remco

---

<div class="post-metadata">

**Author:** ![remco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9d8465/32.png) [@remco](https://www.ruby-forum.com/u/remco)\
**Post date:** [March 4, 2008, 5:20am UTC](https://www.ruby-forum.com/t/acces-multiply-databases/130838/2 "2008-03-04T05:20:18Z")

</div>

Rails’ ActiveRecord objects share the db connection of the ARec object  
from which they inherit. Normally, it’s ActiveRecord::Base and all  
the ARec classes share the same connection. You can take advantage of  
this to accomplish what you’re wanting to do…

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

The class above will allow you to define three new db connections in  
database.yml called company\_development, company\_test, and  
company\_production where you can define the connection to your  
company’s db. Then, you can create classes that inherit from _this_  
class that access your company DB (by default the others will still  
refer to your db… or the ones defined by development, test, and  
production in database.yml).

With the examples above…

class Tags \< ActiveRecord::Base  
…  
end

class Cities \< CompanyConnection  
…  
end

On Mar 3, 2:17 pm, Remco S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
