st-elsewhere icon indicating copy to clipboard operation
st-elsewhere copied to clipboard

St. Elsewhere: A gem that adds support for relationships across different databases in ActiveRecord

=== St. Elsewhere

An ActiveRecord plugin to support relationships across different databases

http://emphaticsolutions.com/images/st.elsewhere.jpg

=== 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.}[http://www.google.com/search?hl=en&source=hp&q=multiple+database+connections+rails&aq=f&oq=&aqi=g4]

=== 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}[http://gist.github.com/239853].

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:

http://emphaticsolutions.com/images/has_many_elsewhere.png

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}[http://drtoast.com/] for the great name and thanks to {Tanner Donovan}[http://github.com/ttdonovan] for patches and being the first production customer.