Handle Lazy Evaluation in distribute_reads blocks
masterBecause ActiveRecord uses lazy evaluation, a query might not execute until it is outside the distribute_reads block, causing it to run on the primary instead of the replica.
To ensure a query runs on a replica, call .to_a or .load inside the block. Alternatively, you can enable automatic loading for all relations returned from distribute_reads blocks by setting DistributeReads.eager_load = true in an initializer.
# This might run on primary if not executed immediately
users = distribute_reads { User.where(orders_count: 1) }
# This ensures execution on the replica
users = distribute_reads { User.where(orders_count: 1).to_a }