# Connect multiple DB in rails project using AR

**URL:** <https://www.ruby-forum.com/t/connect-multiple-db-in-rails-project-using-ar/157587>\
**Category:** Rails\
**Created:** [January 13, 2009, 11:46am UTC](https://www.ruby-forum.com/t/connect-multiple-db-in-rails-project-using-ar/157587 "2009-01-13T11:46:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![on9west](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/6f9a4e/32.png) [@on9west](https://www.ruby-forum.com/u/on9west)\
**Post date:** [January 13, 2009, 11:46am UTC](https://www.ruby-forum.com/t/connect-multiple-db-in-rails-project-using-ar/157587/1 "2009-01-13T11:46:10Z")

</div>

Dear all

I have a rails project which database connection is defined in  
database.yml. There is a table call “servers” which have about 40  
records, which is a collection of database setting (i.e. Server.name,  
Server.port, Server.host)

I know the way to establish multiple connection as below…

class A \< ActiveRecord::Base  
end  
A.establish\_connection(a) # a is the database config

class B \< ActiveRecord::Base  
end  
B.establish\_connection(b) # b is the database config

# repeat and repeat…

However, how can I do this in a block? I have no idea to do so…Please  
give me some light.

all\_servers = Server.find(:all)  
all\_servers.each do |s|  
h = {:host =\> “localhost”,  
:adapter =\> “jdbc”,  
:dialect =\> “sybase”,  
:autocommit =\> false,  
:driver =\> “com.sybase.jdbc3.jdbc.SybDataSource”,  
:url =\> “jdbc:sybase:Tds:#{s.host):#{s.port}/LOE\_DB”,  
:username =\> “username”,  
:password =\> “password”}

# How to I write code to establish 40 connections using active record

here??

# class “Server.name” \< ActiveRecord::Base

# end

# “Server.name”.establish\_connection(h)

end

Million thanks  
Valentino

---

<div class="post-metadata">

**Author:** ![on9west](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/6f9a4e/32.png) [@on9west](https://www.ruby-forum.com/u/on9west)\
**Post date:** [January 14, 2009, 12:17am UTC](https://www.ruby-forum.com/t/connect-multiple-db-in-rails-project-using-ar/157587/2 "2009-01-14T00:17:55Z")

</div>

Hi,

not sure what and _why_ are you trying to do so, but looks like you  
might want to use some simple metaprogramming to create model classes.

For example:  
all\_servers = Server.find(:all)  
all\_servers.each do |s|  
h = {:host =\> “localhost”,  
:adapter =\> “jdbc”,  
:dialect =\> “sybase”,  
:autocommit =\> false,  
:driver =\> “com.sybase.jdbc3.jdbc.SybDataSource”,  
:url =\> “jdbc:sybase:Tds:#{s.host):#{s.port}/LOE\_DB”,  
:username =\> “username”,  
:password =\> “password”}

eval(“class #{Server.name} \< ActiveRecord::Base  
end”)  
eval(Server.name).establish\_connection(h)

end

A hack but should work more or less…

On 13 Sty, 11:46, Valentino L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![on9west](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/6f9a4e/32.png) [@on9west](https://www.ruby-forum.com/u/on9west)\
**Post date:** [January 14, 2009, 2:42am UTC](https://www.ruby-forum.com/t/connect-multiple-db-in-rails-project-using-ar/157587/3 "2009-01-14T02:42:02Z")

</div>

Hubert Lepicki wrote:

> eval(“class #{Server.name} \< ActiveRecord::Base  
> end”)  
> eval(Server.name).establish\_connection(h)
> 
> end
> 
> A hack but should work more or less…

Thanks for your help  
The reason I used this approach is I am building a dashboard application  
to monitor over 40 data servers. I need to extract a lot of information  
in those servers and display it through web.

If this approach is not appropriate, could someone suggest alternatives  
to me?

Million thanks  
Valentino
