How to show SQL queries run in the Rails console?

randombits picture randombits · May 29, 2010 · Viewed 68.4k times · Source

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?

Answer

John Topley picture John Topley · May 29, 2010

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)