# How do I globally switch the database connection?

**URL:** <https://www.ruby-forum.com/t/how-do-i-globally-switch-the-database-connection/125946>\
**Category:** Rails\
**Created:** [January 14, 2008, 2:15am UTC](https://www.ruby-forum.com/t/how-do-i-globally-switch-the-database-connection/125946 "2008-01-14T02:15:52Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![goodieboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ecc23a/32.png) [@goodieboy](https://www.ruby-forum.com/u/goodieboy)\
**Post date:** [January 14, 2008, 2:15am UTC](https://www.ruby-forum.com/t/how-do-i-globally-switch-the-database-connection/125946/1 "2008-01-14T02:15:52Z")

</div>

Hi,

I’m trying to switch from one database to another. Current, I have  
this:

def set\_database  
@client\_id = ‘production’  
config = YAML.load( File.open(File.join(RAILS\_ROOT, ‘config/  
database.yml’)) )  
db = config[@client\_id]  
raise ‘Invalid client id’ if db.nil?  
ActiveRecord::Base.connection =  
ActiveRecord::Base.establish\_connection(db)  
end

Obviously, that’s not working 'cause I’m asking for help! 🙂 No errors  
being thrown, but database is not getting switched.
