title: Rails: making database replication model work with multiple database
body: <p>I have a rails app using 2 databases (say DB1, DB2). Also the DB1 is replicated to DB3, DB2 to DB4. I am using octopus gem for replication i.e. all write operations are on DB1 and read operations from DB3.</p>
  
  <p>Following is my setting for database.yml</p>
  
  <pre><code>production:
    adapter: mysql2
    database: DB1
    username: root
    host: localhost
    password: root
    pool: 5
    timeout: 5000
    reconnect: true
  
  DB2_production:
    adapter: mysql2
    database: DB2
    username: root
    host: localhost
    password: root
    pool: 5
    timeout: 5000
    reconnect: true
  </code></pre>
  
  <p>Now I have file shards.yml as following</p>
  
  <pre><code>octopus:
    replicated: true
    environments:
      - production
      - DB2_production
  
    production:
      slave1:
        adapter: mysql2
        database: DB3
        username: root
        host: localhost
        password: root
        pool: 5
        timeout: 5000
        reconnect: true
  
    DB2_production:
      slave2:
        adapter: mysql2
        database: DB4
        username: root
        host: localhost
        password: root
        pool: 5
        timeout: 5000
        reconnect: true
  </code></pre>
  
  <p>Also I am using 'octopus_establish_connection' to connect to DB2 in my model</p>
  
  <pre><code>class MyModel1 &lt; ActiveRecord::Base
    octopus_establish_connection "DB2_#{Rails.env.to_s}"
  end
  
  
  class MyModel2 &lt; ActiveRecord::Base
    octopus_establish_connection "DB2_#{Rails.env.to_s}"
  end
  </code></pre>
  
  <p>So here if there is a read query from MyModel1 the DB3 is queried and for write query DB1 is queried.
  But in MyModel2 for both cases DB2 is queried, while DB4 should be queried.</p>
  
  <p>Thanks</p>