-
-
Notifications
You must be signed in to change notification settings - Fork 263
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
Add new Rails/WhereMissing
cop
#744
Conversation
Looks like this will only catch code where the chaining is in the exact order prescribed here but order doesn’t matter in ActiveRecord. None of these will be caught for example (and obviously there are many more forms): Foo.where(foos: { id: nil }).left_joins(:foo)
# => Foo.where.missing(:foo)
Foo.left_joins(:foo).where(foos: { id: nil }, x: y)
# => Foo.where(x: y).where.missing(:foo)
Foo.left_joins(:foo).joins(:bar).where(foos: { id: nil })
# => Foo.joins(:bar).where.missing(:foo) |
@ydah Ping. |
Oh, thank you so much. I'll work on it this week. |
I updated this PR. Thank you so much. |
@ydah I think this cop can have autocorrection. Can you implement it? |
I updated this PR. Auto-correction is now supported. Thank you so much. |
I've run on a real code base and got
It happens on
|
Oops, that case was leaking. Thank you so much. |
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.
Sorry to be the bringer of bad news, but there are potential false positives in this cop.
Also with 2 offences within the same file, it managed to replace the left_joins with where.missing, but removed only one of the where(foos: {id: nil}) clauses.
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.
Thank you. The cop no longer crashes or produces false positives, but there are false negatives. I guess we can't easily fix all of those though.
I left come final comments
d0c2953
to
fe2ecf8
Compare
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 found no false positives, false negatives or incorrect corrections
Refs: https://rails.rubystyle.guide/#finding-missing-relationship-records This cop is check for the use `left_joins` and `where` method for finding missing relationship records. ```ruby # bad Foo.left_joins(:foo).where(foos: { id: nil }) # good Foo.where.missing(:foo) ```
Refs: https://rails.rubystyle.guide/#finding-missing-relationship-records
This cop is check for the use
left_joins
andwhere
methodfor finding missing relationship records.
Before submitting the PR make sure the following are checked:
[Fix #issue-number]
(if the related issue exists).master
(if not - rebase it).bundle exec rake default
. It executes all tests and runs RuboCop on its own code.{change_type}_{change_description}.md
if the new code introduces user-observable changes. See changelog entry format for details.