Ruby-on-rails 如何显示在 Rails 控制台中运行的 SQL 查询?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2936000/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:43:19  来源:igfitidea点击:

How to show SQL queries run in the Rails console?

ruby-on-railsactiverecord

提问by randombits

When I run queries (e.g. MyModel.where(...)or record.associated_things) in the console, how can I see the actual database queries being run so I can gain more understanding of what is happening?

当我在控制台中运行查询(例如MyModel.where(...)record.associated_things)时,我如何才能看到正在运行的实际数据库查询,以便更好地了解正在发生的事情?

回答by John Topley

Rails 3+

导轨 3+

Enter this line in the console:

在控制台中输入这一行:

ActiveRecord::Base.logger = Logger.new(STDOUT)

Rails 2

导轨 2

Enter this line in the console:

在控制台中输入这一行:

ActiveRecord::Base.connection.instance_variable_set :@logger, Logger.new(STDOUT)

回答by Abhi

In Rails 3+ you can use ActiveRecord::Relation's to_sqlmethod:

在 Rails 3+ 中,您可以使用 ActiveRecord::Relation 的to_sql方法:

User.where(:id => 3).to_sql
#=> "SELECT \"users\".* FROM \"users\"  WHERE \"users\".\"id\" = 3"

回答by lakesare

There is an .explainmethod in Rails 4.
(.to_sqlworks too, but won't show includes)

.explainRails 4 中有一个方法。
.to_sql也可以使用,但不会显示包含)

Category.includes(:products).explain
=> EXPLAIN for: SELECT "categories".* FROM "categories" 0|0|0|SCAN TABLE categories

EXPLAIN for: SELECT "categories_products".* FROM "categories_products" WHERE "categories_products"."category_id" IN (1, 2) 0|0|0|SCAN TABLE categories_products

EXPLAIN for: SELECT "products".* FROM "products" WHERE "products"."id" IN (1, 2, 3, 4, 5, 6, 7) 0|0|0|SEARCH TABLE products USING INTEGER PRIMARY KEY (rowid=?) 0|0|0|EXECUTE LIST SUBQUERY 1

回答by Aleksandar Pavi?

As from recently, you can use this:

从最近开始,你可以使用这个:

https://github.com/dejan/rails_panel

https://github.com/dejan/rails_panel

It consists of developer console panel add-on for chrome, and gem file which needs to be added to your application's Gemfilelike this:

它由 chrome 的开发者控制台面板插件和需要添加到应用程序的Gemfile 中的gem 文件组成,如下所示:

group :development do
  gem 'meta_request'
end

Then run again:

然后再次运行:

bundle install

Restart your application, open it, and launch developer console, and you should see it like this: enter image description here

重新启动您的应用程序,打开它,然后启动开发者控制台,您应该会看到如下所示: 在此处输入图片说明

回答by kaleb4eg

Starting from Rails 6 there is more convenient approach: simply add ActiveRecord::Base.verbose_query_logs = truein console and you will see all SQL calls and places where it was called. More info https://guides.rubyonrails.org/debugging_rails_applications.html#verbose-query-logs

从 Rails 6 开始,有更方便的方法:只需ActiveRecord::Base.verbose_query_logs = true在控制台中添加,您将看到所有 SQL 调用以及调用它的位置。更多信息https://guides.rubyonrails.org/debugging_rails_applications.html#verbose-query-logs

回答by Huy Vo

I prefer to set up logger level in config/application.rb:

我更喜欢在以下位置设置记录器级别config/application.rb

config.after_initialize do
  Rails.logger.level = (ENV['LOG_LEVEL'] || Logger::INFO).to_i
end

On production my ENV['LOG_LEVEL']will be set to the value of Logger::INFOand on my local machine it'll be Logger::DEBUG.

在生产中,我ENV['LOG_LEVEL']将被设置为 的值,Logger::INFO在我的本地机器上它将是Logger::DEBUG.