module ActiveRecord::ConnectionHandling

This is monkeypatching activerecord to ensure that whenever a new connection is established it switches to the same tenant as before the connection switching. This problem is more evident when using read replica in Rails 6

Public Instance Methods

connected_to(database: nil, role: nil, prevent_writes: false, &blk)
Also aliased as: connected_to_without_tenant
connected_to_with_tenant(database: nil, role: nil, prevent_writes: false) { |blk| ... } click to toggle source
# File lib/apartment/active_record/connection_handling.rb, line 8
def connected_to_with_tenant(database: nil, role: nil, prevent_writes: false, &blk)
  current_tenant = Apartment::Tenant.current

  connected_to_without_tenant(database: database, role: role, prevent_writes: prevent_writes) do
    Apartment::Tenant.switch!(current_tenant)
    yield(blk)
  end
end
Also aliased as: connected_to
connected_to_without_tenant(database: nil, role: nil, prevent_writes: false, &blk)
Alias for: connected_to