Skip to content

Instantly share code, notes, and snippets.

@benjaminoakes
Created April 14, 2017 23:56
Show Gist options
  • Save benjaminoakes/6e78c0a4651f51b5d0177a3eb6fbed75 to your computer and use it in GitHub Desktop.
Save benjaminoakes/6e78c0a4651f51b5d0177a3eb6fbed75 to your computer and use it in GitHub Desktop.
# Debugging helper, useful while fixing N+1 problems
# Based on https://gist.github.com/crazymykl/b526e0ed6b285b3c4e77
def count_queries(log: false, &block)
ActiveRecord::Base.logger = Logger.new(STDOUT) if log
query_count = 0
increment_count = -> (*) { query_count += 1 }
ActiveSupport::Notifications.subscribed(increment_count, 'sql.active_record', &block)
query_count
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment