# Dynamic connection

**URL:** <https://www.ruby-forum.com/t/dynamic-connection/103705>\
**Category:** Rails\
**Created:** [May 9, 2007, 2:58pm UTC](https://www.ruby-forum.com/t/dynamic-connection/103705 "2007-05-09T14:58:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bazzel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/76d3ee/32.png) [@bazzel](https://www.ruby-forum.com/u/bazzel)\
**Post date:** [May 9, 2007, 2:58pm UTC](https://www.ruby-forum.com/t/dynamic-connection/103705/1 "2007-05-09T14:58:31Z")

</div>

Hello all,

In my Rails app I have a model which I want to use to maintain records  
in several tables (identical structure) in multiple databases.  
Is there a way to use establish\_connection and set\_table\_name in a  
dynamic way?

---

<div class="post-metadata">

**Author:** ![bazzel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/76d3ee/32.png) [@bazzel](https://www.ruby-forum.com/u/bazzel)\
**Post date:** [May 9, 2007, 3:33pm UTC](https://www.ruby-forum.com/t/dynamic-connection/103705/2 "2007-05-09T15:33:42Z")

</div>

Yes, there is. establish\_connection and _not_ set\_table\_name is the  
answer.  
I’ve been down both routes and establish\_connection is much lighter on  
the  
necessary code than set\_table\_name.

RSL

---

<div class="post-metadata">

**Author:** ![bazzel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/76d3ee/32.png) [@bazzel](https://www.ruby-forum.com/u/bazzel)\
**Post date:** [May 9, 2007, 4:28pm UTC](https://www.ruby-forum.com/t/dynamic-connection/103705/3 "2007-05-09T16:28:32Z")

</div>

Russell N. wrote:

> Yes, there is. establish\_connection and _not_ set\_table\_name is the  
> answer.  
> I’ve been down both routes and establish\_connection is much lighter on  
> the  
> necessary code than set\_table\_name.
> 
> RSL

Hi RSL,

Thanks for answering. However, I think I need both set\_table\_name and  
establish\_connection I want to access multiple tables on multiple  
databases. Maybe I can overload the ActiveRecord class methods create,  
save, etc. by defining extra parameters for both connection and  
table\_name:  
`
def self.create(attributes, connection, table_name)
set_table_name = table_name
establish_connection connection
super(attributes)
end
`  
but I haven’t tried this appraach yet.

---

<div class="post-metadata">

**Author:** ![bazzel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/76d3ee/32.png) [@bazzel](https://www.ruby-forum.com/u/bazzel)\
**Post date:** [May 10, 2007, 10:13am UTC](https://www.ruby-forum.com/t/dynamic-connection/103705/4 "2007-05-10T10:13:37Z")

</div>

I figured out that it’s actually pretty easy to set connection and  
tablename dynamically by using establish\_connection and set\_table\_name  
for my model only:  
…  
Site.establish\_connection some\_method  
Site.set\_table\_name another\_method  
Site.create (or whatever class method)

In my situation I want to update several MSSQL records on several  
servers. The default database is mySQL.  
However… since I use the RedHill lpugin  
([http://www.redhillonrails.org/#redhillonrails\_core](http://www.redhillonrails.org/#redhillonrails_core)) it seems that there  
is a conflict when I try to create or update on MSSQL when my default  
connection connects to mySQL:  
NoMethodError: undefined method `[]=' for #<Mysql> from c:/DEV/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.2/lib/active_record/connection_adapters/sqlserver_adapter.rb:439:in `indexes’  
from  
./script/…/config/…/config/…/vendor/plugins/redhillonrails\_core/lib/red\_hill\_consulting/core/active\_record/base.rb:42:in  
`indexes' from ./script/../config/../config/../vendor/plugins/redhillonrails_core/lib/red_hill_consulting/core/active_record/base.rb:27:in `columns’  
from  
c:/DEV/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.2/lib/active\_record/base.rb:776:in  
`column_names' from c:/DEV/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.2/lib/active_record/base.rb:789:in `column\_methods\_hash’  
from  
c:/DEV/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.2/lib/active\_record/base.rb:1263:in  
`all_attributes_exists?' from (irb):9:in `all?’  
from  
c:/DEV/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.2/lib/active\_record/base.rb:1263:in  
`each' from c:/DEV/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.2/lib/active_record/base.rb:1263:in `all?’  
from  
c:/DEV/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.2/lib/active\_record/base.rb:1263:in  
`all_attributes_exists?' from c:/DEV/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.2/lib/active_record/base.rb:1197:in `method\_missing’  
from (irb):9

The story continues, but thanks for the reply!

Patrick

---

<div class="post-metadata">

**Author:** ![bazzel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/76d3ee/32.png) [@bazzel](https://www.ruby-forum.com/u/bazzel)\
**Post date:** [May 9, 2007, 4:41pm UTC](https://www.ruby-forum.com/t/dynamic-connection/103705/5 "2007-05-09T16:41:45Z")

</div>

Here’s what I did. Site model is in the default db and contains a  
db\_name  
attribute which holds the name of the db for that site. In Site…

DB\_PREFIX = “whatever\_”

def ensure\_connection  
[Attachment, Document, Category, Tag].each do |klass|  
unless ActiveRecord::Base.configurations.has\_key? db\_name  
config = ActiveRecord::Base.configurations[RAILS\_ENV]  
ActiveRecord::Base.configurations[db\_name] = {  
:adapter =\> “mysql”,  
:host =\> config[“host”],  
:username =\> config[“username”],  
:password =\> config[“password”],  
:database =\> “#{DB\_PREFIX}#{db\_name}”  
}  
end  
[Attachment, Category, Comment, Document, Tag, Source].each do  
|model|  
if model.connection.config[:database] != “#{DB\_PREFIX}#{db\_name}”  
model.establish\_connection db\_name  
# You have to do this manually…  
model.connection.logger = RAILS\_DEFAULT\_LOGGER  
end  
end  
end  
end

and in my controllers that need to access that data just create a  
before\_filter which calls Site#ensure\_connections. Unless you’re on to  
something I wasn’t set\_table\_name is more trouble than it’s worth. Hope  
that  
helps.

RSL
