class ActiveRecord::Base

Public Class Methods

connection() click to toggle source
# File lib/knockoff/active_record/base.rb, line 6
def connection
  target = Thread.current[:knockoff] || Knockoff.default_target
  case target
  when :replica
    # Attempts to use a random replica connection, but otherwise falls back to primary
    Knockoff.replica_pool.random_replica_connection.original_connection
  when :primary, NilClass
    original_connection
  else
    raise Knockoff::Error, "Invalid target: #{Thread.current[:knockoff]}"
  end
end
Also aliased as: original_connection
on_replica() click to toggle source

Generate scope at top level e.g. User.on_replica

# File lib/knockoff/active_record/base.rb, line 20
def on_replica
  # Why where(nil)?
  # http://stackoverflow.com/questions/18198963/with-rails-4-model-scoped-is-deprecated-but-model-all-cant-replace-it
  context = where(nil)
  context.knockoff_target = :replica
  context
end
original_connection()
Alias for: connection