📝Daily

Day 67

Tony Duong

Tony Duong

Aug 24, 20261 min

Also available in:🇫🇷🇯🇵
#engineering#rails#rspec#sql#n+1#testing#active-record
Day 67

Today, I:

  • saved a simple Rails/RSpec helper for counting SQL queries inside a block — useful for verifying that preloading prevents N+1 queries (SCHEMA and CACHE queries are excluded)
# frozen_string_literal: true

# Helper to count SQL queries executed within a block.
# Useful for verifying that preloading prevents N+1 queries.
#
# Usage:
#   query_count = count_queries { some_code }
#   expect(query_count).to eq(0)
module QueryCounter
  # Counts the number of SQL queries executed within the given block.
  # SCHEMA and CACHE queries are excluded from the count.
  #
  # @yield the block whose SQL queries will be counted
  # @return [Integer] the number of SQL queries executed
  #
  # @example Verify that preloaded associations do not trigger additional queries
  #   query_count = count_queries { chat.each(&:messages) }
  #   expect(query_count).to eq(0)
  def count_queries(&)
    count = 0
    counter = lambda do |_name, _start, _finish, _id, payload|
      count += 1 unless payload[:name].in?(%w[SCHEMA CACHE])
    end

    ActiveSupport::Notifications.subscribed(counter, "sql.active_record", &)
    count
  end
end

RSpec.configure do |config|
  config.include QueryCounter
end
Tony Duong

By Tony Duong

A digital diary. Thoughts, experiences, and reflections.