Allow ActiveRecord::QueryMethods#pluck
to accept hash args with symbol & string values
#51676
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Motivation / Background
Extends the feature added in #51565 to allow symbol and string hash values when plucking a single column for a specific table. When trying out this new feature I naturally thought this was already valid seeing as the API is common in other query methods (with differing functionality based on the method) e.g.
includes(foo: :bar)
,order(foo: :desc)
.Detail
Updates
ActiveRecord::QueryMethods#arel_columns_from_hash
which is used by#pluck
to simply return the Arel attribute for the column by default if the hash value is a symbol or string, while maintaining#select
specific aliasing functionality.Additional information
While working on this I realised that #51565 introduced a bit of a non-obvious side effect where a query like:
would return the correct results, but also alias the column in the query (
"comments"."id" AS foo
).Just thought it was worth mentioning in case this is classified as a 'bug'. If so, it might be worth separating out the
#select
specific logic from the rest (or#pluck
from the rest), which would also simplify the conditional logic for the extension in this PR.cc/ @fatkodima Keen for your thoughts on this feature and my last note.
Checklist
Before submitting the PR make sure the following are checked:
[Fix #issue-number]