-
Notifications
You must be signed in to change notification settings - Fork 21.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added documentation for ActiveRecord::Relation#pick
[skip ci]
#48404
Conversation
|
||
[`pick`][] can be used to query single or multiple columns from the underlying table of a model. It accepts a list of column names as an argument and returns the first one of the specified column values with corresponding data type. | ||
|
||
pick is a short-hand for `relation.limit(1).pluck(*column_names).first`, which makes it possible to replace code like: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pick is a short-hand for `relation.limit(1).pluck(*column_names).first`, which makes it possible to replace code like: | |
`pick` is a short-hand for `relation.limit(1).pluck(*column_names).first`, which makes it possible to replace code like: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i will handle this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also created a PR to change the ActiveRecord::Relation#pluck
guide.
f5c23e7
to
870f7bb
Compare
@p8 |
@@ -2256,6 +2256,24 @@ irb> assoc.unscope(:includes).pluck(:id) | |||
|
|||
[`pluck`]: https://api.rubyonrails.org/classes/ActiveRecord/Calculations.html#method-i-pluck | |||
|
|||
### `pick` | |||
|
|||
[`pick`][] can be used to query single or multiple columns from the underlying table of a model. It accepts a list of column names as an argument and returns the first one of the specified column values with corresponding data type. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this was copied and modified from the documentation of pluck
.
The explanation in the API docs is a lot clearer to me.
What do you think about using some of the API docs wording?
For example:
[`pick`][] can be used to query single or multiple columns from the underlying table of a model. It accepts a list of column names as an argument and returns the first one of the specified column values with corresponding data type. | |
[`pick`][] can be used to pick the value(s) from the named column(s) in the current relation. This is primarily useful when you have a relation that’s already narrowed down to a single row. It accepts a list of column names as an argument and returns the first one of the specified column values with corresponding data type. |
PS. I'd also accept a seperate PR to reword the first paragraph of pluck
to something more similar to the API docs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sounds good!
Indeed, I would change this as I thought the wording in the API docs was clearer.
And, I will create a separate PR for changes to pluck
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Supported by 900bf10
Changed the description to use some of the wording from the API documentation. Also, Added nuances to the main useful cases.
```ruby | ||
Customer.limit(1).pluck(:id).first | ||
``` | ||
|
||
with: | ||
|
||
```ruby | ||
Customer.pick(:id) | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The example shows how easy it is to shorten the code with pick.
But I think it misses the nuance that pick "is primarily useful when you have a relation that’s already narrowed down to a single row.".
```ruby | |
Customer.limit(1).pluck(:id).first | |
``` | |
with: | |
```ruby | |
Customer.pick(:id) | |
``` | |
```ruby | |
Customer.where(id: 1).pluck(:id).first | |
``` | |
with: | |
```ruby | |
Customer.where(id: 1).pick(:id) | |
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed, an important nuance was missing. I will reflect the suggested source code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Supported by 9904db0
- Add nuances of the primarily useful cases to the code examples - Changed description to use some wording from API docs
b717293
to
48a26f5
Compare
thank you for making sure.I have modified your last suggestion. |
Thanks @soartec-lab ! |
Motivation / Background
This Pull Request has been created because there is no explanation of
ActiveRecord::Relation#pick
in the guide.Detail
This Pull Request changes to add the documentation for
ActiveRecord::Relation#pick
to the guide.Additional information
Checklist
Before submitting the PR make sure the following are checked:
[Fix #issue-number]