Debugging & learning SQL in the rails console

Public, Databases, Troubleshooting, Other Languages


Created: Oct 23, 2014     Updated: Nov 19, 2015

Sometimes examining the SQL generated by ActiveRecord query and finder methods can be useful in debugging and learning SQL. Rails' ActiveRecord provides the to_sql method to make this easy.

Open up the rails console with rails c or rails console, then enter the query you want to see the SQL for, then just add .to_sql to the end, e.g.