ActiveRecord::Connections provides a new way to manage multi-tenant applications based on multiples databases.
Install the activerecord-connections gem:
gem install activerecord-connections
Add this line to Gemfile:
Runs a bundle install:
ActiveRecord::Connections add this syntax to ActiveRecord::Base:
::Base.using_connection(connection_name, connection_spec) do # Use database connection inside this block end
If you are using Rails, you could use this way:
class ApplicationController < ActionController::Base around_filter :handle_customer protected def handle_customer(&block) customer = Customer.find_by_domain!(request.domain) customer.using_connection(&block) end end class Customer < ::Base serialize :connection_spec def using_connection(&block) ::Base.using_connection(id, connection_spec, &block) end end
- Do not manage activerecord migrations for different databases
- Similar objects of different connections do not differ
- Coverage is not the best of the world (you could help easily)
- You will lost the connection if you call using_connection nested
Sharding and replication
Need sharding or replication? Check out one of these bad boys:
Copyright (c) 2011 Gabriel Sobrinho, released under the MIT license.