# Switch database based on input request

**URL:** <https://www.ruby-forum.com/t/switch-database-based-on-input-request/105625>\
**Category:** Rails\
**Created:** [May 31, 2007, 8:26pm UTC](https://www.ruby-forum.com/t/switch-database-based-on-input-request/105625 "2007-05-31T20:26:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![akull82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/85f322/32.png) [@akull82](https://www.ruby-forum.com/u/akull82)\
**Post date:** [May 31, 2007, 8:26pm UTC](https://www.ruby-forum.com/t/switch-database-based-on-input-request/105625/1 "2007-05-31T20:26:56Z")

</div>

Hi,

is there any solution to switch the database for active\_record releated  
on request input ?  
i try to code an application, where the user can enter the db connect  
string for the request in an for.  
i don’t konwn all databases, so it’s not possible to write the connect  
strings in an file whitch will be readed at startup ☹

i need sometime like

def do\_something  
connection = create\_connection( :params )  
set\_active\_record\_connection\_for\_thread( connection )  
…  
Person.find\_by\_sql  
…  
connection.close  
end

Can anyone help,

AndrÃ©

---

<div class="post-metadata">

**Author:** ![akull82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/85f322/32.png) [@akull82](https://www.ruby-forum.com/u/akull82)\
**Post date:** [May 31, 2007, 9:35pm UTC](https://www.ruby-forum.com/t/switch-database-based-on-input-request/105625/2 "2007-05-31T21:35:04Z")

</div>

ActiveRecord classes hold connection info. In your case, you’ll want  
something like:

def action\_name  
Person.establish\_connection(connection\_params)  
Person.find…  
end

Of course, there’s a lot of ways you can do this, and you’ll need to  
make  
sure that connections don’t stay open or don’t run into each other.  
Another  
note: subclasses of AR::Base share connection information, and any class  
down the hierarchy can define it’s own connection params.

Jason

---

<div class="post-metadata">

**Author:** ![akull82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/85f322/32.png) [@akull82](https://www.ruby-forum.com/u/akull82)\
**Post date:** [June 1, 2007, 5:28pm UTC](https://www.ruby-forum.com/t/switch-database-based-on-input-request/105625/3 "2007-06-01T17:28:03Z")

</div>

Hi Jason,

i think establish\_connection will set the connection as static variable  
for all instances of this type. whats goning on, if an concurrent  
request calls establish\_connection too ?!?

Andre

Jason R. wrote:

> ActiveRecord classes hold connection info. In your case, you’ll want  
> something like:
> 
> def action\_name  
> Person.establish\_connection(connection\_params)  
> Person.find…  
> end
> 
> Of course, there’s a lot of ways you can do this, and you’ll need to  
> make  
> sure that connections don’t stay open or don’t run into each other.  
> Another  
> note: subclasses of AR::Base share connection information, and any class  
> down the hierarchy can define it’s own connection params.
> 
> Jason
