Skip to content

Rails/WhereMissing fails on join that calls a method #1206

Closed
@tinynumbers

Description

Seeing two errors in the Rails/WhereMissing inspections in my codebase.

Here are the two spots in the code where it fails:

model_1.rb:

    def query
      other_models
        .left_outer_joins(left_outer_joins)
        #----------------^ fails here
    end

other_models is a related has_many model; left_outer_joins is a method on the same model.

model_2.rb:

      def call
        initial_query
          .joins(left_joins)
        #-------^ fails here
      end

initial_query and left_joins are methods on the same model.


Expected behavior

The two examples above should not cause Rails/WhereMissing to fail.

Actual behavior

Running Rubocop results in the following output:

2 errors occurred:
An error occurred while Rails/WhereMissing cop was inspecting /.../app/models/model_1.rb:36:26.
An error occurred while Rails/WhereMissing cop was inspecting /.../app/models/model_2.rb:17:17.

No cops failed and all other inspections run properly.

Steps to reproduce the problem

  1. Create a minimal Rails app (rails new app_name --minimal)
  2. Add rubocop and robocop-rails to the group :development, :test in Gemfile and run bundle install:
     gem 'rubocop', require: false
     gem 'rubocop-rails', require: false
    
  3. Add a minimal .rubocop.yml as follows:
    require:
      - rubocop-rails
    
    AllCops:
      TargetRubyVersion: 2.7
      NewCops: enable
    
    Style/Documentation:
      Enabled: false
    Metrics/CyclomaticComplexity:
      Enabled: false
    Metrics/PerceivedComplexity:
      Enabled: false
    Metrics/MethodLength:
      Enabled: false
    Layout/LineLength:
      Enabled: false
  4. Add two models:
    bin/rails generate model user name:string
    bin/rails generate model product name:string user:references
    
  5. Auto-correct with bundle exec rubocop -A - lots of corrections, but no errors.
  6. Modify the User model as follows:
    class User < ApplicationRecord
      has_many :products, dependent: :nullify
    
      def query
        products.left_outer_joins(left_outer_joins)
        #------------------------^ fails here
      end
    
      private
    
      def left_outer_joins
        []
      end
    end
  7. Run bundle exec rubocop and you'll see the error:
❯ bundle exec rubocop
Inspecting 32 files
................................

32 files inspected, no offenses detected
An error occurred while Rails/WhereMissing cop was inspecting /Users/abcde/rubocop-error/app/models/user.rb:7:30.
To see the complete backtrace run rubocop -d.

1 error occurred:
An error occurred while Rails/WhereMissing cop was inspecting /Users/abcde/rubocop-error/app/models/user.rb:7:30.
Errors are usually caused by RuboCop bugs.
Please, report your problems to RuboCop's issue tracker.
https://github.com/rubocop/rubocop/issues

Mention the following information in the issue report:
1.58.0 (using Parser 3.2.2.4, rubocop-ast 1.30.0, running on ruby 2.7.8) +server [arm64-darwin22]```

## RuboCop version

1.58.0 (using Parser 3.2.2.4, rubocop-ast 1.30.0, running on ruby 2.7.8) +server [arm64-darwin22]
  - rubocop-rails 2.22.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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions