# Re: Changing a model's database connection

**URL:** <https://www.ruby-forum.com/t/re-changing-a-models-database-connection/191855>\
**Category:** Rails\
**Created:** [July 19, 2010, 9:32pm UTC](https://www.ruby-forum.com/t/re-changing-a-models-database-connection/191855 "2010-07-19T21:32:35Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![gracei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f6c823/32.png) [@gracei](https://www.ruby-forum.com/u/gracei)\
**Post date:** [July 19, 2010, 9:32pm UTC](https://www.ruby-forum.com/t/re-changing-a-models-database-connection/191855/1 "2010-07-19T21:32:35Z")

</div>

Michaël wrote:

> I want to be able to set those models to use a database on a  
> different server.  
> …  
> I’ve tried the following, but it doesn’t seem to have any effect:

I do this for switching between a legacy db and a new db. It works  
fine. But I was doing it a slightly different way and it.

I think the simplest way is to define a new database in your  
config/database.yml file. You should be able to pass in all of the  
parameters inline too but that tends not to be DRY as you will almost  
certainly need to refer to it more than once. So having it in one  
place in the database.yml is nice and only once. Something like this:

myotherdb:  
adapter: mysql  
username: myusername  
password: mypassword  
database: myotherdatabase

Then to always use it for a model then refer to it by name using  
establish\_connection. Like this:

class User \< ActiveRecord::Base  
establish\_connection “myotherdb”  
…  
end

Since you only want to change to it sometimes dynamically depending  
upon what you are doing then you can call establish\_connection inline  
and switch over from one db to the other. Since you are manually  
explicitly changing db’s out from under activerecord then don’t forget  
to switch back to the standard one afterward. Not doing that was a  
source of bugs in my own projects. The symptom was that sometimes,  
depending upon the program flow, it would end up talking to the wrong  
database.

def build\_report  
ActiveRecord::Base.establish\_connection(“myotherdb”) if !  
ENV[‘RAILS\_ENV’] == ‘test’  
… do your other db stuff here …  
ActiveRecord::Base.establish\_connection(ENV[“RAILS\_ENV”]) if !  
ENV[‘RAILS\_ENV’] == ‘test’  
end

Note the use of ENV[“RAILS\_ENV”] to select the right database when  
returning. That assumes that you are using the standard “production”  
and “development” names. Note the avoidance of this switch when  
running tests. Two gotchas that I tripped over when first setting  
this up.

Also, an unrelated side remark, ruby isn’t python and you don’t need  
to end lines with colons, as you had done in your example. 🙂

Bob

P.S. Sorry if the thread of this message is broken. I had a personal  
snafu and needed to recreate the message from parts.

---

<div class="post-metadata">

**Author:** ![gracei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f6c823/32.png) [@gracei](https://www.ruby-forum.com/u/gracei)\
**Post date:** [July 19, 2010, 11:22pm UTC](https://www.ruby-forum.com/t/re-changing-a-models-database-connection/191855/2 "2010-07-19T23:22:39Z")

</div>

On 19 July, 20:30, Bob P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> config/database.yml file. You should be able to pass in all of the  
> Then to always use it for a model then refer to it by name using  
> explicitly changing db’s out from under activerecord then don’t forget
> 
> Note the use of ENV[“RAILS\_ENV”] to select the right database when  
> returning. That assumes that you are using the standard “production”  
> and “development” names. Note the avoidance of this switch when  
> running tests. Two gotchas that I tripped over when first setting  
> this up.

Thanks for the clarification. This is exactly what I’ve done, and it’s  
working now. I was struggling because there was a rogue backgroundrb  
task running for reports generation that still had the primary server  
configured for its database connection.

> Also, an unrelated side remark, ruby isn’t python and you don’t need  
> to end lines with colons, as you had done in your example. 🙂

I just noticed that 🙂 It’s not in my code fortunately. That’s what  
comes of trying to manage too many projects in different languages.

Thanks for all the help, guys.
