Skip to content

Commit

Permalink
Prefer named placeholders in queries with multiple placeholders
Browse files Browse the repository at this point in the history
  • Loading branch information
bbatsov committed Feb 20, 2015
1 parent a35a9c8 commit 3c4a2d7
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,25 @@ programming resources.
Client.where('orders_count = ?', params[:orders])
```

* <a name="named-placeholder"></a>
Consider using named placeholders instead of positional placeholders
when you have more than 1 placeholder in your query.
<sup>[[link](#named-placeholder)]</sup>

```Ruby
# okish
Client.where(
'created_at >= ? AND created_at <= ?',
params[:start_date], params[:end_date]
)
# good
Client.where(
'created_at >= :start_date AND created_at <= :end_date',
start_date: params[:start_date], end_date: params[:end_date]
)
```

* <a name="find"></a>
Favor the use of `find` over `where`
when you need to retrieve a single record by id.
Expand Down

0 comments on commit 3c4a2d7

Please sign in to comment.