# Same code different databases?

**URL:** <https://www.ruby-forum.com/t/same-code-different-databases/168074>\
**Category:** Rails\
**Created:** [May 15, 2009, 3:02pm UTC](https://www.ruby-forum.com/t/same-code-different-databases/168074 "2009-05-15T15:02:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![simonsaid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b5e925/32.png) [@simonsaid](https://www.ruby-forum.com/u/simonsaid)\
**Post date:** [May 15, 2009, 3:02pm UTC](https://www.ruby-forum.com/t/same-code-different-databases/168074/1 "2009-05-15T15:02:01Z")

</div>

Hello,

We want to serve multiple clients using the same logic but having a  
separate database for each client. We are using capistrano to deploy  
our web application. Anybody has any idea how we could do this? Thanks!

---

<div class="post-metadata">

**Author:** ![simonsaid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b5e925/32.png) [@simonsaid](https://www.ruby-forum.com/u/simonsaid)\
**Post date:** [May 15, 2009, 4:34pm UTC](https://www.ruby-forum.com/t/same-code-different-databases/168074/2 "2009-05-15T16:34:17Z")

</div>

I’m using a different subdomain for each tenant, so could we choose  
the database in rails depending on the subdomain?

---

<div class="post-metadata">

**Author:** ![simonsaid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b5e925/32.png) [@simonsaid](https://www.ruby-forum.com/u/simonsaid)\
**Post date:** [May 18, 2009, 6:48pm UTC](https://www.ruby-forum.com/t/same-code-different-databases/168074/3 "2009-05-18T18:48:09Z")

</div>

Anybody =/

---

<div class="post-metadata">

**Author:** ![simonsaid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b5e925/32.png) [@simonsaid](https://www.ruby-forum.com/u/simonsaid)\
**Post date:** [May 18, 2009, 8:13pm UTC](https://www.ruby-forum.com/t/same-code-different-databases/168074/4 "2009-05-18T20:13:43Z")

</div>

On 18 May 2009, at 17:47, Kentor wrote:

> Anybody =/

you could try calling ActiveRecord::Base.establish\_connection from a  
before\_filter. Re-establishing connections like that every request  
might be horribly slow.

Fred

---

<div class="post-metadata">

**Author:** ![simonsaid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b5e925/32.png) [@simonsaid](https://www.ruby-forum.com/u/simonsaid)\
**Post date:** [May 22, 2009, 2:17pm UTC](https://www.ruby-forum.com/t/same-code-different-databases/168074/5 "2009-05-22T14:17:45Z")

</div>

Hello,

I followed this great guide at  
[http://magicmodels.rubyforge.org/magic\_multi\_connections](http://magicmodels.rubyforge.org/magic_multi_connections)  
.

This guide works perfectly for one model. But I think if I were to use  
it for another one, I would have to duplicate the code for each  
model…  
For my subdomain problem, the way I think it should be done is have a  
global function which will run for all objects, look at the subdomain  
and set the connection but I’m not sure where to put what piece of  
code to achieve this.

Also, when I applied the guide ([http://magicmodels.rubyforge.org/](http://magicmodels.rubyforge.org/)  
magic\_multi\_connections) to an existing application (http://  
[Getting Started with Rails — Ruby on Rails Guides](http://guides.rubyonrails.org/getting_started.html) → it is a guide to create  
a blog) it would work perfectly but only if I comment these lines out:

```
        #link_to 'Show', post
        #link_to 'Edit', edit_post_path(post)
        #link_to 'Destroy', post, :confirm => 'Are you

```

sure?', :method =\> :delete

otherwise, if the connection is other than default and the 1st or 3rd  
line is present I get a this:

undefined method `private\_testing\_post\_path’ for #\<ActionView::Base:  
0x6b08550\>

I used the name “Module PrivateTesting” instead of “Module Private”…

I’m pretty sure there is an obvious fix for my problem (either the  
subdomain or the one with the errors I’m getting and maybe I will be  
able to figure it out from there) but I’m new to Rails and I’ve spent  
many hours trying to get this to work without any success. Any help  
would be really really appreciated. Thank you!

On May 18, 2:12 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
