Created
April 14, 2017 23:56
-
-
Save benjaminoakes/6e78c0a4651f51b5d0177a3eb6fbed75 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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