Skip to content

Commit

Permalink
Added documentation for ActiveRecord::Relation#pick [skip ci]
Browse files Browse the repository at this point in the history
- Add nuances of the primarily useful cases to the code examples
- Changed description to use some wording from API docs
  • Loading branch information
soartec-lab committed Jun 19, 2023
1 parent 54ce634 commit 48a26f5
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions guides/source/active_record_querying.md
Expand Up @@ -2256,6 +2256,25 @@ irb> assoc.unscope(:includes).pluck(:id)

[`pluck`]: https://api.rubyonrails.org/classes/ActiveRecord/Calculations.html#method-i-pluck

### `pick`

[`pick`][] can be used to pick the value(s) from the named column(s) in the current relation. It accepts a list of column names as an argument and returns the first row of the specified column values ​​with corresponding data type.
`pick` is an short-hand for `relation.limit(1).pluck(*column_names).first`, which is primarily useful when you already have a relation that is limited to one row.

`pick` makes it possible to replace code like:

```ruby
Customer.where(id: 1).pluck(:id).first
```

with:

```ruby
Customer.where(id: 1).pick(:id)
```

[`pick`]: https://api.rubyonrails.org/classes/ActiveRecord/Calculations.html#method-i-pick

### `ids`

[`ids`][] can be used to pluck all the IDs for the relation using the table's primary key.
Expand Down

0 comments on commit 48a26f5

Please sign in to comment.