Tuesday, March 16, 2010

Display ActiveRecord generated SQL queries in the console

If you want the console to display the SQL query that ActiveRecord executes just do the following (before you do anything else in the console):
$ script/console
>> ActiveRecord::Base.logger = Logger.new(STDOUT)
=> #
>> User.first
User Load (3.3ms) SELECT * FROM "users" ORDER BY last_name, first_name ASC LIMIT 1
Each call to ActiveRecord now logs all of its activity to STDOUT (e.g., the console), via your custom logger instance.

1 comment:

Anonymous said...

Is it possible to automate this? Hate forgetting to do it the before anything else :)