Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rails/HasManyOrHasOneDependent blows up for statements with a scope proc #539

Closed
omadanathan opened this issue Sep 9, 2021 · 1 comment
Labels
duplicate This issue or pull request already exists

Comments

@omadanathan
Copy link

Our codebase has a number of has_many and has_one lines that use callable procs to limit their scopes. (see the Scopes section of https://api.rubyonrails.org/v6.1.4/classes/ActiveRecord/Associations/ClassMethods.html#method-i-has_many)

has_many :active_participant_tag_assignments, -> { active }, class_name: 'ParticipantTagAssignment', foreign_key: 'participant_id', dependent: :destroy, inverse_of: :participant

As of rubocop-rails 2.12.0, every has_many and has_one call using a scope generates a Rubocop error.


Expected behavior

The Rubocop successfully processes has_many and has_one calls with this format

Actual behavior

An error occurred while Rails/HasManyOrHasOneDependent cop was inspecting /Users/nathan.wilmes/workspace/kairos/app/models/account.rb:83:2.

undefined method `first' for nil:NilClass
/Users/nathan.wilmes/.rvm/gems/ruby-2.7.1@kairos/gems/rubocop-rails-2.12.0/lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb:117:in `valid_options?'
/Users/nathan.wilmes/.rvm/gems/ruby-2.7.1@kairos/gems/rubocop-rails-2.12.0/lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb:80:in `on_send'
/Users/nathan.wilmes/.rvm/gems/ruby-2.7.1@kairos/gems/rubocop-1.20.0/lib/rubocop/cop/commissioner.rb:136:in `public_send'
/Users/nathan.wilmes/.rvm/gems/ruby-2.7.1@kairos/gems/rubocop-1.20.0/lib/rubocop/cop/commissioner.rb:136:in `block (2 levels) in trigger_restricted_cops'
/Users/nathan.wilmes/.rvm/gems/ruby-2.7.1@kairos/gems/rubocop-1.20.0/lib/rubocop/cop/commissioner.rb:160:in `with_cop_error_handling'
/Users/nathan.wilmes/.rvm/gems/ruby-2.7.1@kairos/gems/rubocop-1.20.0/lib/rubocop/cop/commissioner.rb:135:in `block in trigger_restricted_cops'
/Users/nathan.wilmes/.rvm/gems/ruby-2.7.1@kairos/gems/rubocop-1.20.0/lib/rubocop/cop/commissioner.rb:134:in `each'
/Users/nathan.wilmes/.rvm/gems/ruby-2.7.1@kairos/gems/rubocop-1.20.0/lib/rubocop/cop/commissioner.rb:134:in `trigger_restricted_cops'
/Users/nathan.wilmes/.rvm/gems/ruby-2.7.1@kairos/gems/rubocop-1.20.0/lib/rubocop/cop/commissioner.rb:70:in `on_send'
/Users/nathan.wilmes/.rvm/gems/ruby-2.7.1@kairos/gems/rubocop-ast-1.11.0/lib/rubocop/ast/traversal.rb:137:in `block in on_dstr'
/Users/nathan.wilmes/.rvm/gems/ruby-2.7.1@kairos/gems/rubocop-ast-1.11.0/lib/rubocop/ast/traversal.rb:137:in `each'
/Users/nathan.wilmes/.rvm/gems/ruby-2.7.1@kairos/gems/rubocop-ast-1.11.0/lib/rubocop/ast/traversal.rb:137:in `on_dstr'
/Users/nathan.wilmes/.rvm/gems/ruby-2.7.1@kairos/gems/rubocop-1.20.0/lib/rubocop/cop/commissioner.rb:71:in `on_begin'
/Users/nathan.wilmes/.rvm/gems/ruby-2.7.1@kairos/gems/rubocop-ast-1.11.0/lib/rubocop/ast/traversal.rb:154:in `on_class'
/Users/nathan.wilmes/.rvm/gems/ruby-2.7.1@kairos/gems/rubocop-1.20.0/lib/rubocop/cop/commissioner.rb:71:in `on_class'
/Users/nathan.wilmes/.rvm/gems/ruby-2.7.1@kairos/gems/rubocop-ast-1.11.0/lib/rubocop/ast/traversal.rb:20:in `walk'
/Users/nathan.wilmes/.rvm/gems/ruby-2.7.1@kairos/gems/rubocop-1.20.0/lib/rubocop/cop/commissioner.rb:86:in `investigate'
/Users/nathan.wilmes/.rvm/gems/ruby-2.7.1@kairos/gems/rubocop-1.20.0/lib/rubocop/cop/team.rb:155:in `investigate_partial'
/Users/nathan.wilmes/.rvm/gems/ruby-2.7.1@kairos/gems/rubocop-1.20.0/lib/rubocop/cop/team.rb:83:in `investigate'
/Users/nathan.wilmes/.rvm/gems/ruby-2.7.1@kairos/gems/rubocop-1.20.0/lib/rubocop/runner.rb:310:in `inspect_file'
/Users/nathan.wilmes/.rvm/gems/ruby-2.7.1@kairos/gems/rubocop-1.20.0/lib/rubocop/runner.rb:254:in `block in do_inspection_loop'
/Users/nathan.wilmes/.rvm/gems/ruby-2.7.1@kairos/gems/rubocop-1.20.0/lib/rubocop/runner.rb:288:in `block in iterate_until_no_changes'
/Users/nathan.wilmes/.rvm/gems/ruby-2.7.1@kairos/gems/rubocop-1.20.0/lib/rubocop/runner.rb:281:in `loop'
/Users/nathan.wilmes/.rvm/gems/ruby-2.7.1@kairos/gems/rubocop-1.20.0/lib/rubocop/runner.rb:281:in `iterate_until_no_changes'
/Users/nathan.wilmes/.rvm/gems/ruby-2.7.1@kairos/gems/rubocop-1.20.0/lib/rubocop/runner.rb:250:in `do_inspection_loop'
/Users/nathan.wilmes/.rvm/gems/ruby-2.7.1@kairos/gems/rubocop-1.20.0/lib/rubocop/runner.rb:130:in `block in file_offenses'
/Users/nathan.wilmes/.rvm/gems/ruby-2.7.1@kairos/gems/rubocop-1.20.0/lib/rubocop/runner.rb:155:in `file_offense_cache'
/Users/nathan.wilmes/.rvm/gems/ruby-2.7.1@kairos/gems/rubocop-1.20.0/lib/rubocop/runner.rb:129:in `file_offenses'
/Users/nathan.wilmes/.rvm/gems/ruby-2.7.1@kairos/gems/rubocop-1.20.0/lib/rubocop/runner.rb:67:in `block in warm_cache'
/Users/nathan.wilmes/.rvm/gems/ruby-2.7.1@kairos/gems/parallel-1.20.1/lib/parallel.rb:509:in `call_with_index'
/Users/nathan.wilmes/.rvm/gems/ruby-2.7.1@kairos/gems/parallel-1.20.1/lib/parallel.rb:477:in `process_incoming_jobs'
/Users/nathan.wilmes/.rvm/gems/ruby-2.7.1@kairos/gems/parallel-1.20.1/lib/parallel.rb:459:in `block in worker'
/Users/nathan.wilmes/.rvm/gems/ruby-2.7.1@kairos/gems/parallel-1.20.1/lib/parallel.rb:450:in `fork'
/Users/nathan.wilmes/.rvm/gems/ruby-2.7.1@kairos/gems/parallel-1.20.1/lib/parallel.rb:450:in `worker'
/Users/nathan.wilmes/.rvm/gems/ruby-2.7.1@kairos/gems/parallel-1.20.1/lib/parallel.rb:441:in `block in create_workers'
/Users/nathan.wilmes/.rvm/gems/ruby-2.7.1@kairos/gems/parallel-1.20.1/lib/parallel.rb:440:in `each'
/Users/nathan.wilmes/.rvm/gems/ruby-2.7.1@kairos/gems/parallel-1.20.1/lib/parallel.rb:440:in `each_with_index'
/Users/nathan.wilmes/.rvm/gems/ruby-2.7.1@kairos/gems/parallel-1.20.1/lib/parallel.rb:440:in `create_workers'
/Users/nathan.wilmes/.rvm/gems/ruby-2.7.1@kairos/gems/parallel-1.20.1/lib/parallel.rb:380:in `work_in_processes'
/Users/nathan.wilmes/.rvm/gems/ruby-2.7.1@kairos/gems/parallel-1.20.1/lib/parallel.rb:283:in `map'
/Users/nathan.wilmes/.rvm/gems/ruby-2.7.1@kairos/gems/parallel-1.20.1/lib/parallel.rb:232:in `each'
/Users/nathan.wilmes/.rvm/gems/ruby-2.7.1@kairos/gems/rubocop-1.20.0/lib/rubocop/runner.rb:67:in `warm_cache'
/Users/nathan.wilmes/.rvm/gems/ruby-2.7.1@kairos/gems/rubocop-1.20.0/lib/rubocop/runner.rb:46:in `run'
/Users/nathan.wilmes/.rvm/gems/ruby-2.7.1@kairos/gems/rubocop-1.20.0/lib/rubocop/cli/command/execute_runner.rb:26:in `block in execute_runner'
/Users/nathan.wilmes/.rvm/gems/ruby-2.7.1@kairos/gems/rubocop-1.20.0/lib/rubocop/cli/command/execute_runner.rb:52:in `with_redirect'
/Users/nathan.wilmes/.rvm/gems/ruby-2.7.1@kairos/gems/rubocop-1.20.0/lib/rubocop/cli/command/execute_runner.rb:25:in `execute_runner'
/Users/nathan.wilmes/.rvm/gems/ruby-2.7.1@kairos/gems/rubocop-1.20.0/lib/rubocop/cli/command/execute_runner.rb:17:in `run'
/Users/nathan.wilmes/.rvm/gems/ruby-2.7.1@kairos/gems/rubocop-1.20.0/lib/rubocop/cli/command.rb:11:in `run'
/Users/nathan.wilmes/.rvm/gems/ruby-2.7.1@kairos/gems/rubocop-1.20.0/lib/rubocop/cli/environment.rb:18:in `run'
/Users/nathan.wilmes/.rvm/gems/ruby-2.7.1@kairos/gems/rubocop-1.20.0/lib/rubocop/cli.rb:71:in `run_command'
/Users/nathan.wilmes/.rvm/gems/ruby-2.7.1@kairos/gems/rubocop-1.20.0/lib/rubocop/cli.rb:78:in `execute_runners'
/Users/nathan.wilmes/.rvm/gems/ruby-2.7.1@kairos/gems/rubocop-1.20.0/lib/rubocop/cli.rb:47:in `run'
/Users/nathan.wilmes/.rvm/gems/ruby-2.7.1@kairos/gems/rubocop-1.20.0/exe/rubocop:12:in `block in <top (required)>'
/Users/nathan.wilmes/.rvm/rubies/ruby-2.7.1/lib/ruby/2.7.0/benchmark.rb:308:in `realtime'
/Users/nathan.wilmes/.rvm/gems/ruby-2.7.1@kairos/gems/rubocop-1.20.0/exe/rubocop:12:in `<top (required)>'
/Users/nathan.wilmes/.rvm/gems/ruby-2.7.1@kairos/bin/rubocop:23:in `load'
/Users/nathan.wilmes/.rvm/gems/ruby-2.7.1@kairos/bin/rubocop:23:in `<main>'
/Users/nathan.wilmes/.rvm/gems/ruby-2.7.1@kairos/bin/ruby_executable_hooks:22:in `eval'
/Users/nathan.wilmes/.rvm/gems/ruby-2.7.1@kairos/bin/ruby_executable_hooks:22:in `<main>'

Steps to reproduce the problem

  1. Define an active record model with a has_many relationship that incorporates a proc.
  2. Run rubocop on this active record model.

RuboCop version

$ [bundle exec] rubocop -V
1.20.0 (using Parser 3.0.2.0, rubocop-ast 1.11.0, running on ruby 2.7.1 x86_64-darwin19)
  - rubocop-rails 2.12.0
  - rubocop-rspec 2.4.0
@dvandersluis dvandersluis added the duplicate This issue or pull request already exists label Sep 9, 2021
@dvandersluis
Copy link
Member

Duplicate of #535.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

2 participants