Same code different databases?


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!

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

Anybody =/

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.



I followed this great guide at

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
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 (
magic_multi_connections) to an existing application (http://
Getting Started with Rails — Ruby on Rails Guides → 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:

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. [email protected]