Glad You're Ready. Let's Get Started!

Let us know how we can contact you.

Thank you!

We'll respond shortly.

  • Blog Navigation
Monkey Patch Du Jour

The following monkey patch gives a bit more information in the ActiveRecord SQL logs. Instead of just saying “User Load” it also says the file and line number in your code that asked AR to perform the operation. That way you can have a hope of tracking it down and optimizing it away if at all possible.

  User Load (0.2ms) views/main_page.rb:107:in `filters_box'   SELECT * FROM `users`

Code after the jump. I guess you put it in environment.rb with all the other monkeys.

    module ActiveRecord
      module ConnectionAdapters
        class AbstractAdapter
          def log_info(sql, name, ms)
            if @logger && @logger.debug?
              c = caller.detect{|line| line !~ /(activerecord|active_support|__DELEGATION__)/i}
              c.gsub!("#{File.expand_path(File.dirname(RAILS_ROOT))}/", '') if defined?(RAILS_ROOT)
              name = '%s (%.1fms) %s' % [name || 'SQL', ms, c]
              @logger.debug(format_log_entry(name, sql.squeeze(' ')))
  • Gisting:

    Thanks for this, I’m kinda surprised it’s not in rails. I’m gonna play around with it a bit to see if there’s a compelling case for sending it upstream.

  • Alex Chaffee

    Jack – I see you’re playing around with the matching expression. Good call on adding “vendor” — though I’m not sure we wouldn’t want to see that it’s, say, the “acts_as_asinine” plugin that’s calling save 18 times in a row… And I’d leave “lib” out since “lib” often contains application code. I smell a configuration option…

    Here’s the gist URL in clickable form:


  • Alex Chaffee

    As another data point, Rack::Bug has a query report. Its filter is the opposite of the above: it chooses only files that are inside RAILS_ROOT (and not in “vendor”).

    Oh, and (Rack::Bug)[] is amazingly great.

Share This