No release in over 3 years
Low commit activity in last 3 years
A new way to manage multi-tenant applications based on multiples databases
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 2.3.1
>= 0.8.7
>= 1.3.4

Runtime

 Project Readme

ActiveRecord::Connections

ActiveRecord::Connections provides a new way to manage multi-tenant applications based on multiples databases.

Install

Install the activerecord-connections gem:

gem install activerecord-connections

Add this line to Gemfile:

gem 'activerecord-connections'

Runs a bundle install:

bundle install

Usage

ActiveRecord::Connections add this syntax to ActiveRecord::Base:

ActiveRecord::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 < ActiveRecord::Base
  serialize :connection_spec

  def using_connection(&block)
    ActiveRecord::Base.using_connection(id, connection_spec, &block)
  end
end

Known issues

  • 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 lose the connection if you call using_connection nested

Sharding and replication

Need sharding or replication? Check out one of these bad boys:

Copyright

Copyright (c) 2011-2015 Gabriel Sobrinho, released under the MIT license.