# Multiple Active Record Connection

**URL:** <https://www.ruby-forum.com/t/multiple-active-record-connection/162497>\
**Category:** Rails\
**Created:** [March 10, 2009, 3:19pm UTC](https://www.ruby-forum.com/t/multiple-active-record-connection/162497 "2009-03-10T15:19:26Z")\
**Posts on this page:** 2\
**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:** [March 10, 2009, 3:19pm UTC](https://www.ruby-forum.com/t/multiple-active-record-connection/162497/1 "2009-03-10T15:19:26Z")

</div>

Dear all

My ActiveRecord::Base is connected to server\_a (local server), has a  
Server model contains about 100 server records (remote server).

I use the following block to retrieve the server infomation. It works  
fine.

Server.find\_all\_by\_server\_type(“sybase\_t”).each do |s|

```
if s.server_name =~ /^LIS_..._ST1[0-2]/ and s.is_active?
  class Order < ActiveRecord::Base
    set_table_name = "orders"
  end
  Order.establish_connection s.connection_hash

  begin
    puts "#{s.server_name} has #{Order.count} orders"
  rescue
    puts "#{s.server_name} is down"
  end

end

```

end

I want to create another model OrderDetail in the remote server (see  
below code). It does not work. It said “TypeError: superclass mismatch  
for class OrderDetail”.

Server.find\_all\_by\_server\_type(“sybase\_t”).each do |s|  
if s.server\_name =~ /^LIS\_…\_ST1[0-2]/ and s.is\_active?  
class Order \< ActiveRecord::Base  
set\_table\_name = “orders”  
end  
Order.establish\_connection s.connection\_hash  
class OrderDetail \< ActiveRecord::Base  
set\_table\_name = “order\_detail”  
end  
OrderDetail.establish\_connection s.connection\_hash  
begin  
puts “#{s.server\_name} has #{Order.count} orders and  
#{OrderDetail.count} order\_detail”  
rescue  
puts “#{s.server\_name} is down”  
end  
end  
end

I used google and find that the error will happen if the code like this  
[http://railsforum.com/viewtopic.php?id=10993](http://railsforum.com/viewtopic.php?id=10993)

1. class Cool
2. end
3. 
4. class SpecialCool \< Cool
5. end
6. 
7. class SpecialCool # this line will raise an error
8. end

How can I change my code so that I can have more than 1 models in the  
remote server?

Thank you very much  
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:** [March 10, 2009, 8:54pm UTC](https://www.ruby-forum.com/t/multiple-active-record-connection/162497/2 "2009-03-10T20:54:29Z")

</div>

The error that you found in railsforum is probably on the right track.  
Is there any other declaration of the class OrderDetail other than  
what you have shown? If so, this is the problem. Also, if all of the  
servers in you server model have the same set of models, you may want  
to consider the following:

1. Create a RemoteModel class that inherits from ActiveRecord. This  
class will only be used for inheritance and to simplify connections.
2. Create “local” models for each model in the remote server. In your  
example, this would mean creating an order.rb and an order\_details.rb
3. Each of the models created in step 2 should inherit from  
RemoteModel. From your example

class Order \< RemoteModel  
end

1. Your code can then look like this

Server.find\_all\_by\_server\_type(“sybase\_t”).each do |s|  
if s.server\_name =~ /^LIS\_…\_ST1[0-2]/ and s.is\_active?  
RemoteModel. establish\_connection s.connection\_hash  
# Do remote model stuff here  
end  
end

This approach DRY’s up your code and kinda puts things where they  
belong.

On Mar 10, 10:19 am, Valentino L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
