跟踪 Rails 3 SQL 查询
是否有任何适用于 Rails 3 的 gem 可以显示我的代码的哪一部分生成了哪个 SQL 查询?
在 Rails 2.3 上有一个名为 query_trace 的插件,但它似乎不适用于 Rails 3,它会生成以下错误:
alias_method': undefined method `log_info' for class `ActiveRecord::ConnectionAdapters::AbstractAdapter' (NameError)
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
QueryTrace 不能按原样工作,因为 Rails 3 特别是 ActiveRecord 领域进行了许多更改。
所以,我做了一些修改,让它像这样工作:
您只需要在提到的位置提供下面的 2 个文件。然后重新启动网络服务器。
在 SQL 之后,您应该在控制台(白色洋红色)中看到
Called from:
和日志文件In
/vendor/plugins/query_trace/lib/query_trace.rb
In <代码>/vendor/plugins/query_trace/init.rb
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 fileIn
/vendor/plugins/query_trace/lib/query_trace.rb
In
/vendor/plugins/query_trace/init.rb
https://github.com/lightyear/sql-logging
https://github.com/lightyear/sql-logging
query_trace 到rails3 的端口
https://gist.github.com/1137342
A port of query_trace to rails3
https://gist.github.com/1137342
active_record_query_trace 也可以做到这一点。
The active_record_query_trace also does that.
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.这里似乎有一个很好的分叉:
https://github.com/dolzenko/query_trace
请注意文档中的QUERY_TRACE常量似乎不起作用,但是QueryTrace.enable!确实(必须在初始化程序中,我尝试先将其放入 config/development.rb 中)
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)