Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails view activerecord sql statements on debug console

I am using debugger gem in my Rails 3.2 application.

How do I view the resulting SQL statement of active record method calls on the debug console?

For eg: If I run Product.all on the debug console it prints out all the products. I would also like to see the "Select * from Products" as well. Is this possible?

Edit Now that I am wiser(;p) active record also provides .to_sql method on the active record relation objects, to inspect the sql queries that they run against the underlying database.

like image 989
thanikkal Avatar asked Sep 10 '12 06:09

thanikkal


1 Answers

Yep, you bet - just run this before your commands:

ActiveRecord::Base.logger = Logger.new(STDOUT)
like image 95
Brad Werth Avatar answered Nov 20 '22 04:11

Brad Werth