Skip to content
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

Allow ActiveRecord::QueryMethods#pluck to accept hash args with symbol & string values #51676

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

joshuay03
Copy link
Contributor

@joshuay03 joshuay03 commented Apr 27, 2024

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

Post.joins(:comments).pluck(:id, comments: :id)

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:

Post.joins(:comments).pluck(:id, comments: { id: :foo })

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:

  • This Pull Request is related to one change. Unrelated changes should be opened in separate PRs.
  • Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: [Fix #issue-number]
  • Tests are added or updated if you fix a bug or add a feature.
  • CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.

else
field
end
end
end

def arel_columns_from_hash(fields)
def arel_columns_from_hash(fields, for_select: false)
Copy link
Contributor Author

@joshuay03 joshuay03 Apr 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit hacky, see my note in the 'Additional information' section for a possible alternative.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant