# Is there any way to use separated databases for models in one RoR app?

**URL:** <https://www.ruby-forum.com/t/is-there-any-way-to-use-separated-databases-for-models-in-one-ror-app/155026>\
**Category:** Rails\
**Created:** [December 6, 2008, 2:18pm UTC](https://www.ruby-forum.com/t/is-there-any-way-to-use-separated-databases-for-models-in-one-ror-app/155026 "2008-12-06T14:18:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![PaweSSSSSK](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/838e76/32.png) [@PaweSSSSSK](https://www.ruby-forum.com/u/PaweSSSSSK)\
**Post date:** [December 6, 2008, 2:18pm UTC](https://www.ruby-forum.com/t/is-there-any-way-to-use-separated-databases-for-models-in-one-ror-app/155026/1 "2008-12-06T14:18:28Z")

</div>

Like in topic - is there any simple and elegant way to use separated  
databases for specified models in one RoR app?

Here is the example:  
I’d like to have separated DB for user data (model “Account” with  
credentials, account info etc. for security purpose), different one  
for Payment model (date, amount etc.) and third one for (i.e.) store  
data.

Is there any way to do this with maintaining ActiveRecord relations  
between models? (like account.has\_many =\> payments :D)???

Thanks in advance guys 🙂  
Pawe³ Komarnicki, [http://komarnicki.webd.pl](http://komarnicki.webd.pl)

---

<div class="post-metadata">

**Author:** ![PaweSSSSSK](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/838e76/32.png) [@PaweSSSSSK](https://www.ruby-forum.com/u/PaweSSSSSK)\
**Post date:** [December 6, 2008, 3:32pm UTC](https://www.ruby-forum.com/t/is-there-any-way-to-use-separated-databases-for-models-in-one-ror-app/155026/2 "2008-12-06T15:32:24Z")

</div>

On Dec 6, 1:17 pm, Pawe³ K [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Like in topic - is there any simple and elegant way to use separated  
> databases for specified models in one RoR app?
> 
> Here is the example:  
> I’d like to have separated DB for user data (model “Account” with  
> credentials, account info etc. for security purpose), different one  
> for Payment model (date, amount etc.) and third one for (i.e.) store  
> data.  
> see the section titled “Connection to multiple databases in different  
> models” at [Peak Obsession](http://rails.rubyonrails.com/classes/ActiveRecord/Base.html)
> 
> Is there any way to do this with maintaining ActiveRecord relations  
> between models? (like account.has\_many =\> payments :D)???

Probably (unless you try an do things like joins between two databases  
not served by the same database server or stuff like that)

Fred

---

<div class="post-metadata">

**Author:** ![PaweSSSSSK](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/838e76/32.png) [@PaweSSSSSK](https://www.ruby-forum.com/u/PaweSSSSSK)\
**Post date:** [December 6, 2008, 10:45pm UTC](https://www.ruby-forum.com/t/is-there-any-way-to-use-separated-databases-for-models-in-one-ror-app/155026/3 "2008-12-06T22:45:25Z")

</div>

Thanks Fred,

indeed there is some info about that, but… it’s a bit unclear to me  
how to use it… Do I specify establisch\_connection command in model  
file? Or in configuration script?

Thanks for help 🙂  
Pawe³ Komarnicki

---

<div class="post-metadata">

**Author:** ![PaweSSSSSK](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/838e76/32.png) [@PaweSSSSSK](https://www.ruby-forum.com/u/PaweSSSSSK)\
**Post date:** [December 7, 2008, 10:58am UTC](https://www.ruby-forum.com/t/is-there-any-way-to-use-separated-databases-for-models-in-one-ror-app/155026/4 "2008-12-07T10:58:12Z")

</div>

On Dec 6, 10:21 pm, “Maurício Linhares” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> You specify it at your model, eg:
> 
> class Client \< ActiveRecord::Base  
> establish\_connection _put\_your\_config\_here_  
> end
> 
> Then all queries against that model will use it’s connection.

Yup. One tip if you’ve got several models using the same  
configuratrion is that you can do

class Foo \< ActiveRecord::Base  
establish\_connection :other\_database  
end

and that will take the parameters from database.yml for the  
configuration named other\_database.

Fred

---

<div class="post-metadata">

**Author:** ![PaweSSSSSK](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/838e76/32.png) [@PaweSSSSSK](https://www.ruby-forum.com/u/PaweSSSSSK)\
**Post date:** [December 6, 2008, 11:22pm UTC](https://www.ruby-forum.com/t/is-there-any-way-to-use-separated-databases-for-models-in-one-ror-app/155026/5 "2008-12-06T23:22:55Z")

</div>

You specify it at your model, eg:

class Client \< ActiveRecord::Base  
establish\_connection _put\_your\_config\_here_  
end

Then all queries against that model will use it’s connection.

- 

Maurício Linhares  
[http://alinhavado.wordpress.com/](http://alinhavado.wordpress.com/) (pt-br) | [http://blog.codevader.com/](http://blog.codevader.com/)  
(en)

2008/12/6 Pawe³ K [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
