Skip to content

Commit

Permalink
Add bullet for using heredocs with String#squish on multiline SQL
Browse files Browse the repository at this point in the history
  • Loading branch information
kurtzur committed Jul 1, 2015
1 parent 2a72793 commit b139a49
Showing 1 changed file with 27 additions and 0 deletions.
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).
<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

0 comments on commit b139a49

Please sign in to comment.