Git Product home page Git Product logo

activerecord-connections's Introduction

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.