# \[ANN\] uses\_connection plugin

**URL:** <https://www.ruby-forum.com/t/ann-uses-connection-plugin/136703>\
**Category:** Rails\
**Created:** [May 5, 2008, 7:06pm UTC](https://www.ruby-forum.com/t/ann-uses-connection-plugin/136703 "2008-05-05T19:06:09Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thiago\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/58956e/32.png) [@Thiago\_J](https://www.ruby-forum.com/u/Thiago_J)\
**Post date:** [May 5, 2008, 7:06pm UTC](https://www.ruby-forum.com/t/ann-uses-connection-plugin/136703/1 "2008-05-05T19:06:09Z")

</div>

# Description:

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, in other words, this  
is just a nicer way of using “Model.establish\_connection(database)”.

# Example:

First create a ‘shared’ database configuration in config/database.yml:

shared:  
adapter: mysql  
database: shared\_database  
username: user  
password: password  
host: dbs\_host

Next tell your 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 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.

# Where to get it:

GitHb: [http://github.com/railsfreaks/uses\_connection](http://github.com/railsfreaks/uses_connection)  
More info:  
[http://www.railsfreaks.com/2008/5/2/introducing-the-uses\_connection-rails-plugin](http://www.railsfreaks.com/2008/5/2/introducing-the-uses_connection-rails-plugin)

–  
Thiago J.  
[http://www.railsfreaks.com](http://www.railsfreaks.com)
