RSS

View sql queries in your console

This entry was posted on Mar 22 2009

Don’t you think it’s really irritating when, in the console, you are testing your model’s methods and everytime you need to check how each SQL query was formed you need to go to another tab to visualize the log ???

Your frustation is over. Just add the following lines in your .irbrc file:

def log_to
  ActiveRecord::Base.logger = Logger.new($stdout)
  ActiveRecord::Base.connection_pool.clear_reloadable_connections!
end

You can change the method’s name to whatever suits your needs. After that, every time you are in your application’s console you just type log_to and you’re ready to go:

>> log_to
=> []
>> MyModel.count
  SQL (1.8ms) SELECT count(*) as count_all FROM "mymodels"
=> 10

This hint I got it from Pratik Naik while doing pair programming with him at the company I work.

Probably Related posts:

  1. How to Run Just one Test Method from your Terminal
  2. Ultrasphinx bug?
  3. Open Source Rails


2 Responses to “View sql queries in your console”

  1. I guess it only works for the Rails >= 2.2, right?


  2. Hello Matias!
    I didn’t try in older versions of Rails. I’m not sure.


Post a Comment