-
-
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
False Positive for Rails/PluckInWhere #310
Comments
I just stumbled over this false positive as well. Maybe a check needs to be added if the class that |
Fixes rubocop#310. This PR adds `EnforcedStyle` to `Rails/PluckInWhere`. By default, it does not register an offense if `pluck` method's receiver is a variable.
Fixes rubocop#310. This PR adds `EnforcedStyle` to `Rails/PluckInWhere`. By default, it does not register an offense if `pluck` method's receiver is a variable.
Fixes rubocop#310. This PR adds `EnforcedStyle` to `Rails/PluckInWhere`. By default, it does not register an offense if `pluck` method's receiver is a variable.
[Fix #310] Add `EnforcedStyle` to `Rails/PluckInWhere`
we had a situation where this Rubocop rule caused the MySQL query planner to not optimize the query correctly, and overwhelmed the DB.
the correct "select" statement in our case would have been @koic I'm not convinced that this is a good and general Rubocop rule |
@tilo can you provide more details?
This is only true if you've called If you're just nesting relations, it does a subquery. Foo.where(bar_id: Bar.active.select(:id)) This does 1 query: SELECT *
FROM foos
WHERE bar_id IN (
SELECT id FROM bars WHERE active
); If you use pluck, it does 2 queries: Foo.where(bar_id: Bar.active.pluck(:id)) 1 to grab the ids of all the relevant SELECT id FROM bars WHERE active; And 1 to get the SELECT *
FROM foos
WHERE bar_id IN (1, 2, 3); |
Also you said:
But Both of those would do 2 queries -- 1 to select the ids and 1 to grab your rows. Can you provide some relevant code? |
Perhaps what you're meaning to say is that you wanted |
I wanted to use Original PR, using
uses an index Modified PR, after using
This added a sub-query, and the MySQL optimizer did no longer use an index on HugeTable! 😱 e.g. NULL The
this is returning instances of MyModel, instead of numeric IDs Perhaps the auto-corrected query with
and |
PM me for more details :) |
@natematykiewicz it did not result in the same query plan (see above) |
What I was saying is I'm a bit shocked that your subselect query performed so poorly ( @koic Thoughts on marking this cop "unsafe", since there's no guarantee that a subquery will actually perform better than 2 queries? |
@koic WDYT? |
This cop has already been marked as unsafe. OTOH, the doc could be updated. |
Thank you! |
Rails/PluckInWhere assumes that the
pluck
is being called on an ActiveRecord relation.data
is not an ActiveRecord relation, it's an array of hashes. Thepluck
here is using ActiveSupport's Enumerable#pluck.Expected behavior
Call
pluck
on an array of hashes, and not haveRails/PluckInWhere
report an issue.Actual behavior
Call
pluck
on an array of hashes, and haveRails/PluckInWhere
report "Use select instead of pluck within where query method."Steps to reproduce the problem
RuboCop version
Rubocop 0.88.0, Rubocop-Rails 2.6.0
The text was updated successfully, but these errors were encountered: