ChrisInEdmonton
ChrisInEdmonton

Reputation: 4578

How to determine what Ruby-on-Rails code generated some SQL?

I have an application written in Ruby using Rails 2.3. We use ActiveRecord.

Occasionally, ActiveRecord generates some pretty hideously inefficient SQL. We can determine what SQL is problematic using slow-query logs and using new relic. However, it can be really difficult to determine the line of code in our software that generated the problematic SQL. It's generally a query built up using associations and named_scopes.

What I'd really like is some way to tag the SQL in our log files with the filename and line of our code that generated it (not the line of ActiveRecord that executed the query).

The information should be available via the caller array. I don't want to dump the entire array, I just want the filename and line of our code most directly responsible. Is there a gem which already does this? If not, any suggestions?

Upvotes: 5

Views: 1254

Answers (3)

Dennis Krupenik
Dennis Krupenik

Reputation: 676

rack-mini-profiler does include information on which line of ruby code generated which sql query.

Upvotes: 1

rkb
rkb

Reputation: 3542

New Relic will give you full stack traces for individual slow SQL queries with the Slow SQL and Transaction Traces features.

Upvotes: 0

m_x
m_x

Reputation: 12564

you should find some inspiration along the active-record-query-trace gem

EDIT: wooops, read it too fast, this is for RoR 3+. >This< seems to work for rails 2.3. You may also find some inspiration here.

Upvotes: 5

Related Questions