# Two databases one app?

**URL:** <https://www.ruby-forum.com/t/two-databases-one-app/92292>\
**Category:** Rails\
**Created:** [April 17, 2007, 11:06pm UTC](https://www.ruby-forum.com/t/two-databases-one-app/92292 "2007-04-17T23:06:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jawgaping](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ecccb3/32.png) [@jawgaping](https://www.ruby-forum.com/u/jawgaping)\
**Post date:** [April 17, 2007, 11:06pm UTC](https://www.ruby-forum.com/t/two-databases-one-app/92292/1 "2007-04-17T23:06:09Z")

</div>

Ultimately what I need to make happen is have two domains point to the  
same application, however depending on which domain is accessing it to  
use a different database.

I suggested installing the application on two servers, but that option  
is for some reason not good. I tried to install the application twice  
into one account and just symlink them depending on the domain but  
that did not work either, here’s why:

One domain has to point to ~/public\_html, I have no option in that the  
way our host is set up. The second domain has to point to a  
subdirectory of ~/public\_html. Adding a symlink to the first  
application’s public folder didn’t work because Apache is unable to  
follow through two symlinks.

Adding any extra characters to the URL is out of the question too, so  
I can’t use [domain.com/i/app](http://domain.com/i/app)

Ultimately I just need the question answered that is at the top, any  
takers?

---

<div class="post-metadata">

**Author:** ![jawgaping](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ecccb3/32.png) [@jawgaping](https://www.ruby-forum.com/u/jawgaping)\
**Post date:** [April 17, 2007, 11:14pm UTC](https://www.ruby-forum.com/t/two-databases-one-app/92292/2 "2007-04-17T23:14:37Z")

</div>

perhaps one way is to build your models like so:

class MyModel \< ActiveRecord::Base  
establish\_connection(  
:adapter =\> “whatever”,  
:host =\> “database\_#{request.host.gsub(/[^w], ‘\_’)}”,  
:username =\> ‘user’,  
:password =\> ‘pass’  
)  
end

beware i have not tested this, so i’m not sure if this would run with  
every model instantiation

and if the above works, maybe a DRY optimization would be to stuff it  
into ActiveRecord::Base once instead of every model

---

<div class="post-metadata">

**Author:** ![jawgaping](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ecccb3/32.png) [@jawgaping](https://www.ruby-forum.com/u/jawgaping)\
**Post date:** [April 18, 2007, 12:19am UTC](https://www.ruby-forum.com/t/two-databases-one-app/92292/3 "2007-04-18T00:19:41Z")

</div>

On Apr 17, 2007, at 11:03 PM, jawgaping wrote:

> way our host is set up. The second domain has to point to a  
> subdirectory of ~/public\_html. Adding a symlink to the first  
> application’s public folder didn’t work because Apache is unable to  
> follow through two symlinks.
> 
> Adding any extra characters to the URL is out of the question too, so  
> I can’t use [domain.com/i/app](http://domain.com/i/app)
> 
> Ultimately I just need the question answered that is at the top, any  
> takers?

What about having two separate checkouts in the same machine, each  
with its own database.yml. Configure a virtual host for each one in  
Apache. Each virtual host then has its own document root and mongrel  
cluster (each instance of the application runs behind its own cluster).

– fxn

---

<div class="post-metadata">

**Author:** ![jawgaping](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ecccb3/32.png) [@jawgaping](https://www.ruby-forum.com/u/jawgaping)\
**Post date:** [April 18, 2007, 2:52am UTC](https://www.ruby-forum.com/t/two-databases-one-app/92292/4 "2007-04-18T02:52:49Z")

</div>

Although a low level solution would be more preferable, this works  
wonderfully! Thank you jemm you probably saved my job.

---

<div class="post-metadata">

**Author:** ![jawgaping](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ecccb3/32.png) [@jawgaping](https://www.ruby-forum.com/u/jawgaping)\
**Post date:** [April 18, 2007, 10:58am UTC](https://www.ruby-forum.com/t/two-databases-one-app/92292/5 "2007-04-18T10:58:22Z")

</div>

On Apr 18, 2007, at 2:51 AM, jawgaping wrote:

> > ```
> > :adapter => "whatever",
> > :host => "database_#{request.host.gsub(/[^w], '_')}",
> > :username => 'user',
> > :password => 'pass'
> > 
> > ```
> > 
> > )  
> > end

Careful, with that approach MyModel will always point to the database  
of the domain that triggered its class loading. In development mode  
that works because the class is reloaded at each request. However, in  
production mode that means MyModel won’t point to the table of the  
database of the other domain _ever_.

If that’s OK then you have your model layer really partitioned in two  
databases known before hand, and in that case the regular  
establish\_connection + two keys in database.yml will suffice.

Otherwise you’d move that code to a high-priority filter in  
ApplicationController:

def choose\_database\_for\_host  
ActiveRecord::Base.establish\_connection …  
end

because you need to select the connection _per request_. That has a  
performance cost, but perhaps it’s fine with the load of that  
applicaction. If that was not the case more intrusive solutions  
involving some sort of connection pool would be needed.

– fxn

PS: The solution with two separate checkouts with respective mongrel  
clusters didn’t fit?
