Skip to content

New cop to detect where.blank? antipattern #1529

@fatkodima

Description

@fatkodima

blank?/present? on the relation loads the whole relation to determine if there are records in them. empty?/any? does a simple "exists" like query.

# bad
users.where(active: true).present?
users.where(active: true).blank?

users.where(active: true).any?
users.where(active: true).empty?

One of these cases was responsible for 10% of the whole IO on a loaded production database. I also found 20+ other examples of this antipattern in the codebase.

Cop can be named something like Rails/WhereBlank.

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