St. Elsewhere: A gem that adds support for relationships across different databases in ActiveRecord
Switch branches/tags
Nothing to show
Pull request Compare This branch is even with briandoll:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


St. Elsewhere

An ActiveRecord plugin to support relationships across different databases

The Scenario

For a variety of reasons, you might find yourself supporting multiple databases in your Rails application. Maybe you're connecting to a legacy database for a few models. Perhaps you have divided your Rails application into two parts, one database for your online catalog system and another for transactional data. Multiple database connections in Rails is nothing new.

The Problem

While there may be great benefits to connecting to multiple databases in your app, there are also costs. One example is that has_many :children, :through => parent_children may not work.

If your database connections are available on the same host you can prefix your ActiveRecord table names with the database name.

If your database connections are on two different hosts, no JOINs can save you and you'll need to implement your relationships in code.

The Solution

St. Elsewhere adds a new class method (has_many_elsewhere) to support basic association methods across different database connections for ActiveRecord models.


class Hospital < AcitveRecord::Base
  has_many :hospital_doctors
  has_many_elsewhere :doctors, :through => :hospital_doctors

class HospitalDoctor < ActiveRecord::Base
  belongs_to :hospital
  belongs_to :doctor

class TransactionalBase < ActiveRecord::Base
  self.abstract_class = true
  establish_connection "#{RAILS_ENV}-transactional"

class Doctor < TransactionalBase
  has_many :hospital_doctors
  has_many :hospitals, :through => :hospital_doctors

The following conventional methods are available for Hospital:
hospital.doctors, hospital.doctors=, hospital.doctor_ids, hospital.doctor_ids=


has_many_elsewhere is certainly much less efficient than a comparable has_many relationship. has_many :through relationships use SQL JOINs which while efficient, do not work across multiple database connections. St. Elsewhere implements much of the same resulting API methods in code, using less efficient SQL.

Install from gemcutter

gem install st-elsewhere


Currently st-elsewhere is implemented as a basic ruby module that implements some of the basic functionality of has_many :through relationships in ActiveRecord. A much more robust implementation would be to create an ActiveRecord association proxy, like HasManyThroughAssociation, that emulates the same API and could be integrated into the standard has_many class method. I will likely be waiting for Rails 3 to be released (and thus the new base ORM implementation) before attempting the association proxy route.


Thanks to James Reynolds for the great name and thanks to Tanner Donovan for patches and being the first production customer.