How to show SQL queries run in the Rails console?

Ruby on-RailsActiverecord

Ruby on-Rails Problem Overview


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?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

Rails 3+

Enter this line in the console:

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

Rails 2

Enter this line in the console:

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

Solution 2 - Ruby on-Rails

In Rails 3+ you can use ActiveRecord::Relation’s to_sql method:

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

Solution 3 - Ruby on-Rails

There is the .explain method in Rails 4.
(.to_sql works too, but won't show includes)

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

Solution 4 - Ruby on-Rails

Starting from Rails 6 there is more convenient approach: simply add ActiveRecord::Base.verbose_query_logs = true in 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

Solution 5 - Ruby on-Rails

I just wanted to give our production console the same behavior I’m used to on dev, where all SQL queries are reported to the console.

Rails.logger.level = 0

3.0.3 :001 > Rails.logger.level = 0
 => 0
3.0.3 :002 > User.last
  User Load (0.7ms)  SELECT `users`.* FROM `users` ORDER BY `users`.`id` DESC LIMIT 1
 => #<User:…>

Solution 6 - Ruby on-Rails

As from recently, you can use this:

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 Gemfile like this:

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

Solution 7 - Ruby on-Rails

I prefer to set up logger level in 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::INFO and on my local machine it'll be Logger::DEBUG.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionrandombitsView Question on Stackoverflow
Solution 1 - Ruby on-RailsJohn TopleyView Answer on Stackoverflow
Solution 2 - Ruby on-RailsAbhiView Answer on Stackoverflow
Solution 3 - Ruby on-RailslakesareView Answer on Stackoverflow
Solution 4 - Ruby on-Railskaleb4egView Answer on Stackoverflow
Solution 5 - Ruby on-RailsNoach MagedmanView Answer on Stackoverflow
Solution 6 - Ruby on-RailsAleksandar PavićView Answer on Stackoverflow
Solution 7 - Ruby on-RailsHuy VoView Answer on Stackoverflow