You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is your feature request related to a problem? Please describe.
Too often I see folks using pluck as part of a query when they really should choose select. Examples that come most readily to mind are part of where clauses, especially when a subquery is involved. For example, I sometimes see something like
which will be very inefficient as the number of FooFactorys increases, since the ever-larger plucked Ruby array size will slow things down, making the following preferable (if a subquery is necessary at all):
Introduce a Rubocop rule that will find, and possibly auto-correct, usages of pluck (and/or other finder methods??) in a subquery.
Describe alternatives you've considered
It's obviously possible to socialize knowledge about this, and I have tried, but a Rubocop rule will enforce even for those whom such socialization doesn't reach.
Additional context
Please forgive if I'm asking about this in the wrong way or with too little detail. Whereas I'm a huge Rubocop fan and grateful user, this is my first time requesting a feature or considering contributing to Rubocop.
The text was updated successfully, but these errors were encountered:
What about a scenario where you're already selecting on the same model? Widget.select here returns an array of widgets, from which we can pluck(:id) or map(&:id).
Widget.where(id: Widget.select(&:active?).pluck(:id))# Rails/PluckInWhere: Use select instead of pluck within where query method.
The aforementioned scenario works, but Rubocop complains about the pluck. However, replacing it with a select in this case is not the correct solution because Array#select is not the same as ActiveRecord::QueryMethods#select. A straight swap will raise an error:
Widget.where(id: Widget.select(&:active?).select(:id))# ArgumentError: wrong number of arguments (given 1, expected 0)
Should the code be modified to not warn when pluck is called on an Array? Or is there some other Rubocop rule that would help here?
Is your feature request related to a problem? Please describe.
Too often I see folks using
pluck
as part of a query when they really should chooseselect
. Examples that come most readily to mind are part ofwhere
clauses, especially when a subquery is involved. For example, I sometimes see something likewhich will be very inefficient as the number of
FooFactory
s increases, since the ever-largerpluck
ed Ruby array size will slow things down, making the following preferable (if a subquery is necessary at all):Describe the solution you'd like
Introduce a Rubocop rule that will find, and possibly auto-correct, usages of
pluck
(and/or other finder methods??) in a subquery.Describe alternatives you've considered
It's obviously possible to socialize knowledge about this, and I have tried, but a Rubocop rule will enforce even for those whom such socialization doesn't reach.
Additional context
Please forgive if I'm asking about this in the wrong way or with too little detail. Whereas I'm a huge Rubocop fan and grateful user, this is my first time requesting a feature or considering contributing to Rubocop.
The text was updated successfully, but these errors were encountered: