In Rails, how to get the actual request when an ActiveRecord request is executed

I use a simple query in ActiveRecord that does something like this.

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

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

+4
source share
4 answers

You can see the SQL query that executes by looking at the development log/development.log located in log/development.log . Note that the script/server command processes this log file by default.

  • In Rails 3, you can add a .to_sql method .to_sql to the end of the crawler for SQL output.

  • As an alternative, the New Relic free RPM Lite gem allows you to see SQL queries in developer mode, as well as many other useful features, information settings.

+7
source

You can insatll mongrel

it will deliver you all sql requests on your terminal

or run an active write request on script / console

+1
source

In development mode, your database queries are printed on the server console, you can localize the query there.

0
source

You are looking for construct_finder_sql .

  >> User.send (: construct_finder_sql, {: conditions => {: username => 'joe'}})
 => "SELECT * FROM \" users \ "WHERE (\" users \ ". \" Username \ "= E'joe ')"

 >> User.scoped (: joins =>: orders,: conditions => {: username => 'joe'}). Send (: construct_finder_sql, {})
 => "SELECT \" users \ ". * FROM \" users \ "INNER JOIN \" orders \ "ON orders.user_id = users.id WHERE (\" users \ ". \" Username \ "= E'joe ' ) "
0
source

Source: https://habr.com/ru/post/1315902/


All Articles