Skip to content

Instantly share code, notes, and snippets.

@eric-hu
Created August 4, 2011 19:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eric-hu/1126066 to your computer and use it in GitHub Desktop.
Save eric-hu/1126066 to your computer and use it in GitHub Desktop.
Script for ~/.irbrc to enable SQL query output for Rails 3
extend_console 'rails3', defined?(ActiveSupport::Notifications), false do
$odd_or_even_queries = false
ActiveSupport::Notifications.subscribe('sql.active_record') do |*args|
$odd_or_even_queries = !$odd_or_even_queries
color = $odd_or_even_queries ? ANSI[:CYAN] : ANSI[:MAGENTA]
event = ActiveSupport::Notifications::Event.new(*args)
time = "%.1fms" % event.duration
name = event.payload[:name]
sql = event.payload[:sql].gsub("\n", " ").squeeze(" ")
puts " #{ANSI[:UNDERLINE]}#{color}#{name} (#{time})#{ANSI[:RESET]} #{sql}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment