Skip to content

Rails/HasManyOrHasOneDependent false positive when has_one called on a class instance #687

Closed
@estraph

Description

When defining a decorator of a model we sometimes call has_one or has_many on the base class we are decorating:

module Foo
  def self.prepended(base)
    base.has_one :bar, dependent: :destroy
  end
end

Bar.prepend(Foo)

The Rails/HasManyOrHasOneDependent cop still triggers, which seems like a false positive:

app/models/foo.rb:3:10: C: Rails/HasManyOrHasOneDependent: Specify a :dependent option.
      base.has_one :bar, dependent: :destroy
           ^^^^^^^

Expected behavior

The cop should detect that dependent: :destroy is already defined.

Actual behavior

The cop fails suggesting dependent: :destroy is not present.

Steps to reproduce the problem

Call has_one or has_many on a class instance rather than as part of a class definition.

module Foo
  def self.prepended(base)
    base.has_one :bar, dependent: :destroy
  end
end

Bar.prepend(Foo)

RuboCop version

% bundle exec rubocop -V
1.26.1 (using Parser 3.1.1.0, rubocop-ast 1.16.0, running on ruby 3.0.3 x86_64-darwin21)
  - rubocop-performance 1.13.3
  - rubocop-rails 2.14.2

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

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions