Sha256: 51457f3500579310aec5192f6918a7d2e358cd5187c180eef9f587a15ce9732c

Contents?: true

Size: 667 Bytes

Versions: 1

Compression:

Stored size: 667 Bytes

Contents

module ActiveRecordSlave
  module InstanceMethods

    # Database Adapter method #select is called for every select call
    # Replace #select with one that calls the slave connection instead
    def select_with_slave_reader(sql, name = nil, *args)
      # Only read from slave when not in a transaction and when this is not already the slave connection
      if (open_transactions == 0) && (Thread.current[:active_record_slave] != :master)
        ActiveRecordSlave.read_from_master do
          Slave.connection.select(sql, "Slave: #{name || 'SQL'}", *args)
        end
      else
        select_without_slave_reader(sql, name, *args)
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
active_record_slave-1.0.0 lib/active_record_slave/instance_methods.rb