13

Is there any gem which works on Rails 3 that can show which part of my code generated which SQL query?

On Rails 2.3 there was plugin called query_trace, but it doesn't seem to work on Rails 3, it generates following error:

alias_method': undefined method `log_info' for class `ActiveRecord::ConnectionAdapters::AbstractAdapter' (NameError)
spacemonkey
  • 19,664
  • 14
  • 42
  • 62

5 Answers5

12

QueryTrace doesn't work as-is because many changes were made in Rails 3 esp in the area of ActiveRecord.

So, hacking around, I made it work like this:

You just need the 2 files below in the locations mentioned. Then restart the web server. After the SQL, you should see Called from: in a console (magenta on white) and log file

In /vendor/plugins/query_trace/lib/query_trace.rb

module QueryTrace
  def self.append_features(klass)
    super
    klass.class_eval do
      unless method_defined?(:log_info_without_trace)
        alias_method :log_info_without_trace, :sql
        alias_method :sql, :log_info_with_trace
      end
    end
  end

  def log_info_with_trace(event)
    log_info_without_trace(event)
    logger.debug("\e[1m\e[35m\e[1m\e[47mCalled from:\e[0m " + clean_trace(caller[2..-2]).join("\n "))
  end

  def clean_trace(trace)
    Rails.respond_to?(:backtrace_cleaner) ?
      Rails.backtrace_cleaner.clean(trace) :
      trace
  end
end

In /vendor/plugins/query_trace/init.rb

require 'query_trace'

class ::ActiveRecord::LogSubscriber
  include QueryTrace
end
Zabba
  • 64,285
  • 47
  • 179
  • 207
7

https://github.com/lightyear/sql-logging

Bryan Larsen
  • 9,468
  • 8
  • 56
  • 46
  • This appears to be a rewrite of query_trace for rails3. At least, the credits acknowledge the original query_trace. – Rob Jul 03 '13 at 19:37
  • Had really good results with this gem. Not color-coded but the depth and accuracy of the stack trace is excellent. – Joshua Pinter Apr 24 '15 at 15:38
5

A port of query_trace to rails3

https://gist.github.com/1137342

paul.belt
  • 289
  • 3
  • 3
1

The active_record_query_trace also does that.

  1. Add the following to your Gemfile:
group :development do
  gem 'active_record_query_trace'
end
  1. Create an initializer such as config/initializers/active_record_query_trace.rb to enable the gem. If you want to customize how the gem behaves, you can add any combination of the options described in the docs to the initializer as well.
if Rails.env.development?
 ActiveRecordQueryTrace.enabled = true
 # Optional: other gem config options go here
end
  1. Restart the Rails development server.
BrunoF
  • 3,239
  • 26
  • 39
0

There appears to be a good fork here:

https://github.com/dolzenko/query_trace

Note that the QUERY_TRACE constant in the documentation doesn't seem to work, but the QueryTrace.enable! does (must be in an initializer, I tried putting it into config/development.rb first)

Brian Underwood
  • 10,746
  • 1
  • 22
  • 34
  • that gave me a "wrong number of arguments" backtrace, so I don't think it is working for rails 3.2 at least – Rob Jul 03 '13 at 19:38