Showing posts with label console. Show all posts
Showing posts with label console. Show all posts

Tuesday, February 12, 2013

SQL logs in Rails console


While working on different Rails projects, I often find it useful to see behind the scenes SQL queries in the console itself. We can the view the SQL queries in the log file itself, but I find it easier to look at the SQL queries at the console itself.

For this purpose in my Ubuntu 11.10, I have created a file called .irbrc in my home directory and have pasted the below contents in that file.

1:  require 'pp'  
2:  if ENV.include?('RAILS_ENV') && !Object.const_defined?('RAILS_DEFAULT_LOGGER')  
3:       require 'logger'  
4:        RAILS_DEFAULT_LOGGER = Logger.new(STDOUT)  
5:  end  


Now after starting the console, the output for any ActiveRecord operation shows the sql query also

Here is a simple output from the console
1:  >> User.all(:limit => 1)  
2:   User Load (0.9ms)  SELECT * FROM "users" LIMIT 1  

Not, I could test this only in Rails 2.