Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Rails, how to get the actual Query when an ActiveRecord query executes

I am using a simple query in ActiveRecord which does something like this.

MyTable.find(:all, :conditions => {:start_date => format_time(params[:date]) })

I want to get the equivalent query that is executed in the background, perhaps using a puts statement or something similar to that. MySQL is my database.

like image 714
bragboy Avatar asked Feb 27 '23 15:02

bragboy


1 Answers

You can see the SQL query that is executed by viewing the development log located in log/development.log. Note that the script/server command tails this log file by default.

  • In Rails 3 you can append a .to_sql method call to the end of the finder to output the SQL.

  • Alternatively, New Relic's free RPM Lite gem lets you see the SQL queries in developer mode as well as lots of other useful performance tuning information.

like image 151
John Topley Avatar answered May 05 '23 13:05

John Topley