# activerecord multiple databases

**URL:** <https://discuss.rubyonrails.org/t/activerecord-multiple-databases/18575>\
**Category:** rubyonrails-talk\
**Created:** [September 26, 2007, 4:08am UTC](https://discuss.rubyonrails.org/t/activerecord-multiple-databases/18575 "2007-09-26T04:08:01Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![dusty](https://avatars.discourse-cdn.com/v4/letter/d/a183cd/32.png) [@dusty](https://discuss.rubyonrails.org/u/dusty)\
**Post date:** [September 26, 2007, 4:08am UTC](https://discuss.rubyonrails.org/t/activerecord-multiple-databases/18575/1 "2007-09-26T04:08:01Z")

</div>

So, I was looking for a setup that would allow me to easily query multiple databases and not have to re-write my customer finder type queries. Here is what I came up with and it seems to be working for me in my very basic tests so I thought I'd share. Just posting it in case this helps anyone else trying to do the same thing.

I'm using this with activerecord, but not rails, so there may be some adjustments needed if you are trying to incorporate this into rails.

module Project &nbsp;&nbsp;DBCONFIG = YAML::load(File.open('database.yml'))

&nbsp;&nbsp;module ConnectionFactory &nbsp;&nbsp;&nbsp;&nbsp;class DB1 \< ActiveRecord::Base &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.establish\_connection(Project::DBCONFIG['db1']) &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;class DB2 \< ActiveRecord::Base &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.establish\_connection(Project::DBCONFIG['db2']) &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end

&nbsp;&nbsp;module ClassMethods &nbsp;&nbsp;&nbsp;&nbsp;module SomeClassName &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def long\_query(name) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r = find\_by\_sql ["SELECT ...",name] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r.first unless r.empty? &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;module SomeOtherClass &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def long\_query(number) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r = find\_by\_sql ["SELECT ...",name] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r.first unless r.empty? &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end

end

module DB1 &nbsp;&nbsp;class SomeClassName \< Project::ConnectionFactory::DB1 &nbsp;&nbsp;&nbsp;&nbsp;set\_table\_name "thetable" &nbsp;&nbsp;&nbsp;&nbsp;extend Project::ClassMethods::SomeClassName &nbsp;&nbsp;end &nbsp;&nbsp;class SomeOtherClass \< Project::ConnectionFactory::DB1 &nbsp;&nbsp;&nbsp;&nbsp;set\_table\_name "theothertable" &nbsp;&nbsp;&nbsp;&nbsp;extend Project::ClassMethods::SomeOtherClass &nbsp;&nbsp;end end

module DB2 &nbsp;&nbsp;class SomeClassName \< Project::ConnectionFactory::DB2 &nbsp;&nbsp;&nbsp;&nbsp;set\_table\_name "thetable" &nbsp;&nbsp;&nbsp;&nbsp;extend Project::ClassMethods::SomeClassName &nbsp;&nbsp;end &nbsp;&nbsp;class SomeOtherClass \< Project::ConnectionFactory::DB2 &nbsp;&nbsp;&nbsp;&nbsp;set\_table\_name "theothertable" &nbsp;&nbsp;&nbsp;&nbsp;extend Project::ClassMethods::SomeOtherClass &nbsp;&nbsp;end end

# queries db1 DB1::SomeClassName.long\_query(name) DB1::SomeOtherClass.long\_query(name)

# queries db2 DB2::SomeClassName.long\_query(name) DB2::SomeOtherClass.long\_query(name)
