Added documentation for ActiveRecord::Relation#pick [skip ci]

- Add nuances of the primarily useful cases to the code examples
- Changed description to use some wording from API docs
This commit is contained in:
soartec-lab 2023-06-06 15:52:57 +09:00
parent 54ce634a9e
commit 48a26f5073

@ -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.