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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Lun <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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 > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---