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

Warn about subqueries when replacing pluck with pick #1277

Merged
merged 1 commit into from
May 13, 2024

Conversation

thiago-gitlab
Copy link
Contributor

The RuboCop::Cop::Rails::Pick cop "Enforces the use of ‘pickoverpluck(…).first`."

When using this cop on an existing code base, it's not possible for the cop to detect whether the relation already has a limit applied.

In these cases, following the advice will cause a subquery to be added to the resulting query.

Model.limit(100).pluck(:a).first is not equivalent to Model.limit(100).pick(:a). The former generates a SQL query with a LIMIT 100. The latter generates a query with LIMIT 1 and subquery with LIMIT 100.

This proposed documentation update highlights the scenario so the developer can better judge when to obey the cop.

Example: https://gitlab.com/gitlab-org/gitlab/-/issues/455116

Note: I haven't updated docs/modules/ROOT/pages/cops_rails.adoc as I believe this is done when a new version of the gem is cut.


Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.
  • If this is a new cop, consider making a corresponding update to the Rails Style Guide.

@koic koic merged commit 582af1e into rubocop:master May 13, 2024
14 checks passed
@koic
Copy link
Member

koic commented May 13, 2024

Thanks!

@thiago-gitlab thiago-gitlab deleted the rails-pick-description branch May 13, 2024 23:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants