Module: ActiveRecord::ConnectionHandling

Defined in:
lib/apartment/active_record/connection_handling.rb

Overview

This is monkeypatching Active Record 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

Instance Method Summary collapse

Instance Method Details

#connected_to_with_tenant(role: nil, prevent_writes: false, &blk) ⇒ Object Also known as: connected_to



9
10
11
12
13
14
15
16
# File 'lib/apartment/active_record/connection_handling.rb', line 9

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