rubocop-rails icon indicating copy to clipboard operation
rubocop-rails copied to clipboard

[Fix #565] Add cop Rails/WhereNotWithMultipleConditions

Open nhasselmeyer opened this issue 3 years ago • 5 comments

Rails 6.1 changed how where.not works with multiple hash arguments.

This cop identifies places where where.not is used with multiple hash arguments. It doesn't suggest corrections because depending on the rails version, different behaviours may be correct.

# bad
User.where.not(trashed: true, role: 'admin')

# good
User.where.not('trashed = ? OR role = ?', true, 'admin')

Before submitting the PR make sure the following are checked:

  • [x] The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • [x] Wrote good commit messages.
  • [x] Commit message starts with [Fix #issue-number] (if the related issue exists).
  • [x] Feature branch is up-to-date with master (if not - rebase it).
  • [x] Squashed related commits together.
  • [x] Added tests.
  • [x] Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • [x] 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.
  • [x] The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • [ ] If this is a new cop, consider making a corresponding update to the Rails Style Guide - PR: rubocop/rails-style-guide/pull/296

nhasselmeyer avatar Feb 10 '22 16:02 nhasselmeyer

It doesn't suggest corrections because depending on the rails version, different behaviours may be correct.

Yeah, I agree with the incompatible behavior, but I think this cop can provide unsafe auto-correction.

koic avatar Mar 04 '22 10:03 koic

Let's also update the PR title so it shows up when people search for WhereNotWithMultipleConditions.

andyw8 avatar Mar 08 '22 23:03 andyw8

@andyw8 @koic Can one of you have another review for this PR or is it already ready for a merge to master? Thanks!

denzelem avatar Jul 06 '22 15:07 denzelem

@niklas-hasselmeyer I'm sorry for delay. This looks good to me. Can you squash your commits into one?

koic avatar Jul 28 '22 13:07 koic

@koic I squashed my commits and rebased on master, so this can be merged

nhasselmeyer avatar Sep 20 '22 08:09 nhasselmeyer

Thanks!

koic avatar Sep 25 '22 09:09 koic