Skip to content

Expand Rails/FindBy (and FindEach) to cover all code locations #1058

Closed
@gravitystorm

Description

Is your feature request related to a problem? Please describe.

I was trying to figure out why rubocop-rails wasn't picking up where(...).first usage in our application (OpenStreetMap).

Eventually I found that the cop is restricted to models only, in the default configuration.

Rails/FindBy:
Description: 'Prefer find_by over where.first.'
StyleGuide: 'https://rails.rubystyle.guide#find_by'
Enabled: true
VersionAdded: '0.30'
VersionChanged: '2.11'
IgnoreWhereFirst: true
Include:
- app/models/**/*.rb

However I don't think this restriction is necessary, and I think it's actually unhelpful. I suspect that a large amount, perhaps the majority, of ActiveRecord .where usage would be found in controllers, not in models. In our application, it also crops up repeatedly in our tests.

Describe the solution you'd like

I'd like to see the default configuration updated, to remove the Include: ['app/models/**/*.rb'] restriction, for the Rails/FindBy and Rails/FindEach cops.

Describe alternatives you've considered

The alternative is for everyone to override the configuration for these two cops in their own configuration, e.g.

Rails/FindBy:
  Include:
    - '**/*.rb'

Additional context

The Include restriction was originally introduced alongside the initial cop (in rubocop) in 6188d755eefd94875bc155bbfe433f7752d0bd2b, but I can't find any related discussions.

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