sdsykes/read_from_slave — Issue #9: Provide Example for use outside of Rails
Author: falsetto
Created: 2011-12-06 21:49:23+00:00
State: open

Is it possible to use read_from_slave in a standalone environment? I'm trying the following without success (all reads, even inside of `with_primary_slave` go to the master):

`config/database.yml`

```
adapter: mysql
database: master
username: asdf
password: asdf
host: xxx.xxx.xxx.xxx
port: 3306
slaves:
primary_slave: slave_for_reads

slave_for_reads:
adapter: mysql
database: slave
username: asdf
password: asdf
host: xxx.xxx.xxx.xxx
port: 3306
```

`example code`

```ruby

require 'rubygems'
require 'active_record'

db_config = YAML::load(File.open(File.join(File.dirname(__FILE__),'config','database.yml')))
ActiveRecord::Base.establish_connection(db_config)
require 'read_from_slave'

class Member < ActiveRecord::Base; end

# Queries master instead of slave
Member.first
# Also queries master instead of slave
ActiveRecord::Base.with_primary_slave do
Member.first
end
```