Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

43 lines (35 sloc) 2.298 kb
master_slave_adapter - maurício DOT linhares AT gmail DOT com
This simple plugin acts as a common ActiveRecord adapter and allows you to
setup a master-slave environment using any database you like (and is supported
by ActiveRecord).
This plugin works by handling two connections, one to a master database,
that will receive all non-"SELECT" statements, and another to a slave database
that that is going to receive all SELECT statements. It also tries to do as
little black magic as possible, it works just like any other ActiveRecord database
adapter and performs no monkeypatching at all, so it's easy and simple to use
and understand.
The master database connection will also receive SELECT calls if a transaction
is active at the moment or if a command is executed inside a "with_master" block:
ActiveRecord::Base.with_master do # :with_master instructs the adapter
@users = User.all # to use the master connection inside the block
To use this adapter you just have to install the plugin:
ruby script/plugin install git://
And then configure it at your database.yml file:
database: sample_development
username: root
adapter: master_slave # the adapter must be set to "master_slave"
master_slave_adapter: mysql # here's where you'll place the real database adapter name
disable_connection_test: true # this will disable the connection test before use,
# can possibly improve the performance but you could also
# hit stale connections, default is false
eager_load_connections: true # connections are lazy loaded by default, you can load gem eagerly setting this to true
master: # and here's where you'll add the master database configuration
database: talkies_development # you shouldn't specify an "adapter" here, the
username: root # value at "master_slave_adapter" is going to be used
adapter: postgresql # you can use another adapter for the master connection if needed
# if you don't set it the "master_slave_adapter" property will be used
Jump to Line
Something went wrong with that request. Please try again.