Search code examples
ruby-on-railsruby-on-rails-7

ruby on rails 7.1.x law sql comment "loading for pp"


From version 7.1.0.beta1, some comment have added in raw sql statement.

irb(main):002> Rails::VERSION::STRING
=> "7.1.0.beta1"
irb(main):003> Product.limit(2)
  Product Load (0.1ms)  SELECT "products".* FROM "products" /* loading for pp */ LIMIT ?  [["LIMIT", 2]]

What does it mean? and Why that comment have added?


Solution

  • It's just a comment (/* and */ are the sql comment delimiters).

    After the SQL query, you should see the result. In your example you'll see the first two Product records.

    The comment tells you that "this sql query string is not the result of the query, but it's added here for your information". "pp" means "pretty print".

    ActiveRecord just provides this as an aid so you can see the query... otherwise the query result is all you'd see. Remember, Ruby returns the last value of a method as its return value. In the case of your query, it's the first two Product instances.

    You can actually annotate your queries yourself, if it seems useful. For example Product.annotate('fetching the first two').limit(2).