Home>

I'm sorry for the rudimentary question, but ...
Suppose I want to execute the following query on rails console.

User.find (1)

At this time, the SQL that is spit out on the log has the following format.

SELECT "users". * FROM "users" WHERE "users". "Id" =? LIMIT? [["Id", 1], ["LIMIT", 1]]

However, when I actually write the above SQL by myself, I usually think that it will be in the following form.
(Conversely, if the statement output in the above log is executed directly as SQL on the db console etc.Error: near "[[" id ", 1]": syntax errorIt cannot be executed next to it. .. )

SELECT * FROM "users" WHERE "id" = 1 LIMIT 1;

Why is the actual writing format and the log format different here?

Even if you try to check the SQL issued from the log, it is often different from the pure SQL as described above. ..

Can anyone please teach me the above reasons and "references for reading (converting?) SQL output to the log"?

  • Answer # 1

    Reasons for the above and "References for reading (converting?) SQL output to the log"

    It's called a placeholder.
    rails-query basics

    Even if you try to check the SQL issued from the log, it is often different from pure SQL as described above.

    It's a SQL injection measure.
    If you want SQL that can be executed by copy and paste, SQL trace may not be via bind variables, so why not check it?