Skip to content

Rails/PluckId is not excluding array collections #301

Closed
@beetlegius-jt

Description

Expected behavior

No alerts for arrays.

Actual behavior

Rails/PluckId raises an alert when using pluck(:id) with array collections.
Arrays does not support .ids, that only works for ActiveRecord::Relation collections.

Steps to reproduce the problem

I have this code:

array = [{ id: 1 }, { id: 2 }]
extracted_ids = array.pluck(:id)

Rubocop suggestion:

Rails/PluckId: Use ids instead of pluck(:id).
    extracted_ids = array.pluck(:id)
                          ^^^^^^^^^^

Updated code:

array = [{ id: 1 }, { id: 2 }]
extracted_ids = array.ids

Then it produces the following error:

NoMethodError: undefined method `ids' for [{:id=>1}, {:id=>2}]:Array

RuboCop version

bundle exec rubocop -V
0.88.0 (using Parser 2.7.1.4, rubocop-ast 0.2.0, running on ruby 2.7.1 x86_64-linux)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions