# Different database for different domains

**URL:** <https://www.ruby-forum.com/t/different-database-for-different-domains/162255>\
**Category:** Rails\
**Created:** [March 7, 2009, 4:29pm UTC](https://www.ruby-forum.com/t/different-database-for-different-domains/162255 "2009-03-07T16:29:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![konstantin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/858c86/32.png) [@konstantin](https://www.ruby-forum.com/u/konstantin)\
**Post date:** [March 7, 2009, 4:29pm UTC](https://www.ruby-forum.com/t/different-database-for-different-domains/162255/1 "2009-03-07T16:29:31Z")

</div>

Hello!  
1.  
I have 2 domains [site1.com](http://site1.com) and [site2.com](http://site2.com). The same application is  
installed on both, but I need it to use different database:  
db1 for [site1.com](http://site1.com) and db2 for [site2.com](http://site2.com)  
How can I do this?  
2.  
And another question about subdomains.  
Usually I use something like that:  
link\_to ‘Link title’, :sort =\> ‘soft\_field’ - and this produces a link  
to the current url with additonal ?sort=sort\_field parameter:  
[http://site1.com/controller/?sort=-sort\_field](http://site1.com/controller/?sort=-sort_field)  
But this doesn’t work on subdomains when I use subdomain fu, I get  
something like that:  
http:///controller/?sort=-sort\_field  
What is wrong here and how can I corrent this?

Thanks!

---

<div class="post-metadata">

**Author:** ![konstantin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/858c86/32.png) [@konstantin](https://www.ruby-forum.com/u/konstantin)\
**Post date:** [March 8, 2009, 8:20am UTC](https://www.ruby-forum.com/t/different-database-for-different-domains/162255/2 "2009-03-08T08:20:49Z")

</div>

> 1. 
> 
> And another question about subdomains.  
> Usually I use something like that:  
> link\_to ‘Link title’, :sort =\> ‘soft\_field’ - and this produces a link  
> to the current url with additonal ?sort=sort\_field parameter:[http://site1.com/controller/?sort=-sort\_field](http://site1.com/controller/?sort=-sort_field)  
> But this doesn’t work on subdomains when I use subdomain fu, I get  
> something like that:  
> http:///controller/?sort=-sort\_field  
> What is wrong here and how can I corrent this?

This problem was resolved by hacking subdomain\_fu as it’s described in  
this post:  
[http://www.portallabs.com/blog/2008/10/22/fixing-subdomain\_fu-with-named-routes/](http://www.portallabs.com/blog/2008/10/22/fixing-subdomain_fu-with-named-routes/)

But I still don’t know how to use different databases for different  
domains. Any help is highly appreciated.

---

<div class="post-metadata">

**Author:** ![konstantin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/858c86/32.png) [@konstantin](https://www.ruby-forum.com/u/konstantin)\
**Post date:** [March 9, 2009, 1:30pm UTC](https://www.ruby-forum.com/t/different-database-for-different-domains/162255/3 "2009-03-09T13:30:44Z")

</div>

The first question remains, please, give me a key to resolve the  
issue.  
Currently I see only one way:  
symlink all files from one domain to another except /config folder,  
and put custom /config to every domain.  
But there should be more elegant solution!

---

<div class="post-metadata">

**Author:** ![konstantin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/858c86/32.png) [@konstantin](https://www.ruby-forum.com/u/konstantin)\
**Post date:** [March 9, 2009, 2:33pm UTC](https://www.ruby-forum.com/t/different-database-for-different-domains/162255/4 "2009-03-09T14:33:40Z")

</div>

Haven’t done it myself, but have you looked at  
ActiveRecord::Base.establish\_connection?

If you can establish model-specific connections, you should be able to  
establish a specific connection for a specific subdomain.

---

<div class="post-metadata">

**Author:** ![konstantin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/858c86/32.png) [@konstantin](https://www.ruby-forum.com/u/konstantin)\
**Post date:** [March 10, 2009, 7:01am UTC](https://www.ruby-forum.com/t/different-database-for-different-domains/162255/5 "2009-03-10T07:01:33Z")

</div>

On 9 ÜÐà, 16:33, Ar Chron [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Haven’t done it myself, but have you looked at  
> ActiveRecord::Base.establish\_connection?
> 
> If you can establish model-specific connections, you should be able to  
> establish a specific connection for a specific subdomain.

Okay, thanks!  
The main problem is that I don’t know where to put a code for re-  
defining database connection.  
Should it be somewhere in initializers? Will I have an access to host  
name there?  
Also, I have some parameters in config/enviroments/production.rb and  
they should be changed for another domain too
