如何从rails 2中的活动记录打印正在执行的sql
我想打印到日志或控制台正在执行的sql。我可以在哪里进行一些设置?在休眠中,它可能在配置文件中:我认为这里也应该是类似的东西。
我有一个与此类似的代码部分:
myresult = MyActiveRecordClass.find(:all, :select => "mytable1.*, mytable2.field1", :joins => :mytable2, :conditions => "somefield= #{somefield}", :order => "another_field desc", :offset => offset, :limit => limit)
这是一个更简单的手动翻译案例,但我希望将所有本机 sql 放在控制台或文件中。它对于搜索可能很有用
I would like to print into log or console the executing sql. Where can I make some settings? In hibernate it is in a config file possibility: I think here should be something like that too.
I have a code part similar to this:
myresult = MyActiveRecordClass.find(:all, :select => "mytable1.*, mytable2.field1", :joins => :mytable2, :conditions => "somefield= #{somefield}", :order => "another_field desc", :offset => offset, :limit => limit)
This is a simpler case to translate by hand, but I would like to have all the native sql in a console or in file. It could be usefully at searching
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Rails 已经在开发日志和控制台中显示 SQL(在
debug
日志级别 - 因此确保记录器能够显示debug
日志消息)。同时显示源代码中 SQL 的调用位置:
Rails 2: query_trace 插件
Rails 3: 跟踪 Rails 3 SQL 查询(基于 query_trace,但适用于 Rails 3 因为 query_trace 目前不适用于 Rails 3)
Rails already displays the SQL in the development log and console (at
debug
log level - so ensure that the logger is able to displaydebug
log messages).To also display where in the source code a SQL was called from:
Rails 2: query_trace plugin
Rails 3: Tracing Rails 3 SQL queries (based on query_trace, but works for Rails 3 because query_trace does not currently work on Rails 3)