Forum: Ruby on Rails ActiveRecord::Base.establish_connection loads once?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Elias O. (Guest)
on 2009-04-30 22:34
(Received via mailing list)
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}"
    )
Elias O. (Guest)
on 2009-05-01 00:42
(Received via mailing list)
Can I make it load in every request?
pepe (Guest)
on 2009-05-02 00:51
(Received via mailing list)
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
This topic is locked and can not be replied to.