Merge pull request #48404 from soartec-lab/task/add-doc-to-activerecord-pick
Added documentation for `ActiveRecord::Relation#pick` [skip ci]
This commit is contained in:
commit
676fdb1414
@ -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.
|
||||
|
Loading…
Reference in New Issue
Block a user