# Same model over multiple databases problem

**URL:** <https://www.ruby-forum.com/t/same-model-over-multiple-databases-problem/187031>\
**Category:** Rails\
**Created:** [April 16, 2010, 12:13am UTC](https://www.ruby-forum.com/t/same-model-over-multiple-databases-problem/187031 "2010-04-16T00:13:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 16, 2010, 12:13am UTC](https://www.ruby-forum.com/t/same-model-over-multiple-databases-problem/187031/1 "2010-04-16T00:13:27Z")

</div>

Hello,

I run many gameservers (about 10), each gameserver has a different  
database and all gameserver’s databases has the table “players” which  
is associated to the model Player. So every time I want to work with  
the Player model I need connect to a different database depending on  
which gameserver I am working on.

My problem is that I need to make rails choose which database to use  
before creating/loading the model Player. I know which database to use  
because there is a central database with the table players\_map that  
has the name of all players and tells me which gameserver database  
that player is.

So in resume, any ideas how can I make rails to select the database  
connection before working with a model?

Thanks,  
Eduardo Bart

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 16, 2010, 2:58am UTC](https://www.ruby-forum.com/t/same-model-over-multiple-databases-problem/187031/2 "2010-04-16T02:58:34Z")

</div>

database yml:

external\_db:  
adapter: oracle  
database: ext\_db  
username: ext\_user  
password: ext\_password  
host: ext\_host

model:

establish\_connection :external\_db

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 16, 2010, 3:29am UTC](https://www.ruby-forum.com/t/same-model-over-multiple-databases-problem/187031/3 "2010-04-16T03:29:37Z")

</div>

You didn’t get it.

If a make a model like:

class Player \< ActiveRecord::Base  
establish\_connection :gameserver1\_database  
…  
end

Then every Player model will connect to gameserver1\_database and  
retrieve only player from there:  
@player = Player.find(:first)

What I want is something more flexible that allows me to select which  
database to use, like this fake code:  
@player = Player.find(:first, :database =\> :gameserver1\_database)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 16, 2010, 4:11am UTC](https://www.ruby-forum.com/t/same-model-over-multiple-databases-problem/187031/4 "2010-04-16T04:11:10Z")

</div>

Ya I thought about it after I sent it here you go:

look at the bottom:

[http://railsforum.com/viewtopic.php?id=24338](http://railsforum.com/viewtopic.php?id=24338)
