Skip to content
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

Merged
merged 1 commit into from Jul 1, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 27 additions & 0 deletions README.md
Expand Up @@ -658,6 +658,33 @@ when you need to retrieve a single record by some attributes.
# good
User.where.not(id: id)
```
* <a name="squished-heredocs"></a>
When specifying an explicit query in a method such as `find_by_sql`, use
heredocs with `squish`. This allows you to legibly format the SQL with
line breaks and indentations, while supporting syntax highlighting in many
tools (including GitHub, Atom, and RubyMine).
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this paragraph end with <sup>[[link](#squished-heredocs)</sup> ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes yes, thank you

<sup>[[link](#squished-heredocs)]</sup>

```Ruby
User.find_by_sql(<<SQL.squish)
SELECT
users.id, accounts.plan
FROM
users
INNER JOIN
accounts
ON
accounts.user_id = users.id
# further complexities...
SQL
```

[`String#squish`](http://apidock.com/rails/String/squish) removes the indentation and newline characters so that your server
log shows a fluid string of SQL rather than something like this:

```
SELECT\n users.id, accounts.plan\n FROM\n users\n INNER JOIN\n acounts\n ON\n accounts.user_id = users.id
```

## Migrations

Expand Down