ActiveRecord::Base.establish_connection loads once?

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}”
)

Can I make it load in every request?

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