Giter Club home page Giter Club logo

uses_connection's Introduction

uses_connection Rails plugin

This is a fairly simple but handy plugin that lets you specify which database connection to use on a per-model basis. It is useful when you have multiple applications sharing the same data.

Usage Example

First lets create a 'shared' database configuration in config/database.yml:


shared:
  adapter: mysql
  database: shared_database
  username: user
  password: password
  host: dbs_host

Next we tell our model that it should connect the the 'shared' database and use the table there...


class ZipCode < ActiveRecord::Base
  # Uses the 'shared' database in all environments
  uses_connection :shared, :in => :all
end

... and voila, the next time you do


ZipCode.find(:first)

or any other interaction with the ZipCode model it will use the zip_codes table in your shared database! In other words, this is just a nicer way to use "Model.establish_connection(database)".

Documentation

# === options:
# :in:: Accepts an array or a single symbol describing the environment it
#       should use the connection for. Examples:
# 
#       class Book < ActiveRecord::Base
#         # Uses the 'shared' database in all environments
#         uses_connection :shared, :in => :all
# 
#         # Uses the 'shared' database in the production environment
#         uses_connection :shared, :in => :production
# 
#         # Uses the 'shared' database in the production and development environments
#         uses_connection :shared, :in => [:production, :development]
#       end
# 
# 
# :except:: Ignores the call if the current environment matches the array
#           or the single symbol used to describe the environment.
# 
#           class Book < ActiveRecord::Base
#             # Skips the plugin for the development environment
#             uses_connection :shared, :in => :all, :except => :development
# 
#             # Skips the plugin for the development and test environments
#             uses_connection :shared, :in => :all, :except => [:development, :test]
#           end

Author

Thiago Jackiw: tjackiw at gmail dot com

Release Information

Released under the MIT license.

uses_connection's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

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.