Forum: Ruby on Rails Two databases one app?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
jawgaping (Guest)
on 2007-04-18 01:06
(Received via mailing list)
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

Ultimately I just need the question answered that is at the top, any
takers?
Jeff E. (Guest)
on 2007-04-18 01:14
(Received via mailing list)
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
Xavier N. (Guest)
on 2007-04-18 02:19
(Received via mailing list)
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
>
> 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
jawgaping (Guest)
on 2007-04-18 04:52
(Received via mailing list)
Although a low level solution would be more preferable, this works
wonderfully!  Thank you jemm you probably saved my job.
Xavier N. (Guest)
on 2007-04-18 12:58
(Received via mailing list)
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?
This topic is locked and can not be replied to.