# ActiveRecord::Base.establish\_connection loads once?

**URL:** <https://www.ruby-forum.com/t/activerecord-base-establish-connection-loads-once/166910>\
**Category:** Rails\
**Created:** [April 30, 2009, 8:34pm UTC](https://www.ruby-forum.com/t/activerecord-base-establish-connection-loads-once/166910 "2009-04-30T20:34:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![elioncho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ed8c4c/32.png) [@elioncho](https://www.ruby-forum.com/u/elioncho)\
**Post date:** [April 30, 2009, 8:34pm UTC](https://www.ruby-forum.com/t/activerecord-base-establish-connection-loads-once/166910/1 "2009-04-30T20:34:44Z")

</div>

Hello guys,

I’m using the establish connection method on my models to connect to  
different databases. My question is : Is it possible to connect to  
various databases dynamically as I’m trying to do (I am sending a name  
param) or does the connection params are only loaded once and can’t be  
changed?

ActiveRecord::Base.establish\_connection(  
:adapter =\> “sqlite”,  
:database =\> “db\_#{name}”  
)

---

<div class="post-metadata">

**Author:** ![elioncho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ed8c4c/32.png) [@elioncho](https://www.ruby-forum.com/u/elioncho)\
**Post date:** [April 30, 2009, 10:42pm UTC](https://www.ruby-forum.com/t/activerecord-base-establish-connection-loads-once/166910/2 "2009-04-30T22:42:42Z")

</div>

Can I make it load in every request?

---

<div class="post-metadata">

**Author:** ![elioncho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ed8c4c/32.png) [@elioncho](https://www.ruby-forum.com/u/elioncho)\
**Post date:** [May 1, 2009, 10:51pm UTC](https://www.ruby-forum.com/t/activerecord-base-establish-connection-loads-once/166910/3 "2009-05-01T22:51:42Z")

</div>

Hi,

I’ve never done what you are trying but my understanding is that every  
time that your code executes it opens a new connection to the  
database. I just looked at establish\_connection’s code and it doesn’t  
seem to check if there is an existing connection matching the  
parameters passed. I could be wrong, though. I am not a Rails expert.

Since you are going to be trying to create the connection from a Ruby  
program your program will be able to replace your variables with  
values so my guess is that yes, you can do it. Have you tried?

Pepe
