st-elsewhere

This gem provides has_many_elsewhere, an ActiveRecord class method to support many to many relationships in Rails applications, across multiple database connections.


Keywords
activerecord, database-connector, ruby
License
MIT
Install
gem install st-elsewhere -v 0.1.5

Documentation

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.

Example:

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

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

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

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

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

Inefficiencies

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

Roadmap

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

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