New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use Heredocs for multiline SQL #147
Conversation
@@ -658,6 +658,32 @@ when you need to retrieve a single record by some attributes. | |||
# good | |||
User.where.not(id: id) | |||
``` | |||
* <a name="squished-heredocs"</a> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe you're missing a >
from the link.
fc8989d
to
d8d8463
Compare
+1 for this pull request. Readability greatly increases, and you can better follow the flow of the query. A couple weeks ago I had to write a long query, but kept it as a normal string, because I did not know about heredocs. |
d8d8463
to
a4c7a31
Compare
👍 |
1 similar comment
👍 |
SQL | ||
``` | ||
|
||
`squish` removes the indentation and newline characters so that your server |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe this should be String#squish
and be a link to the docs (which might outdated, though).
Looks good overall. I'd just suggest capitalising the commit message and making it a bit more specific, as it's pretty generic right now. |
a4c7a31
to
b139a49
Compare
@bbatsov done |
Perfect! Thanks! |
I didn't see anything in this guide covering the format of multiline SQL strings used with methods like
find_by_sql
orActiveRecord::Base.connection.execute
. I know the preferred approach is to use ActiveRecord helpers, but as even the Rails guides themselves admit, this isn't always sufficient. As such, I decided to include the practice we currently follow at OneLogin.The content I added is pretty self-explanatory, but we use heredocs because they're recommended for multiline strings and enable SQL syntax highlighting in many tools. Because the indentations and line breaks are only meant for readers of the source, the heredoc is immediately followed by
squish
to suppress any extra characters in server logs.