Skip to content

Performance Rails Cop proposal: Replace Pluck with Select within where clauses or subqueries #246

Closed
@ghost

Description

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

Widget.where(id: FooFactory.spectacular.pluck(:widget_id))

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):

Widget.where(id: FooFactory.spectacular.select(:widget_id))

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions