rails/rails — pull request #8230 by panthomakos (2012-11-15 20:29:17+00:00)
Title: Allow models to share a connection pool.

This little feature arose from an actual application where an object hierarchy did not correspond to the database mapping. Instead of establishing a new connection in each model, and creating multiple connection pools and connections, this change allows one model to delegate its connection to another model.

Example:

class User < ActiveRecord::Base; end
class Profile < ActiveRecord::Base; end

class RegularUser < User
establish_connection(:regular_db)
end

class SuperUser < User
establish_connection(:super_db)
end

class RegularProfile < Profile
share_connnection(RegularUser)
end

class SuperProfile < Profile
share_connection(SuperUser)
end

In this example it does not make sense to create a superclass called `RegularDB` that both `RegularUser` and `RegularProfile` inherit from because that does not correspond at all with the object hierarchy. It's much more intuitive to have a top level `User` class, and then simply share the appropriate connection. This also enforces a separation between the database connection and the object hierarchy.