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/RedundantPresenceValidationOnBelongsTo breaks code validation #1319

Closed
wieczorek1990 opened this issue Aug 2, 2024 · 4 comments
Closed

Comments

@wieczorek1990
Copy link

I applied corrections and it broke my code.

Before:

class Appointment < ApplicationRecord
  belongs_to :patient
  validates :patient_id, presence: true, allow_blank: false

After:

class Appointment < ApplicationRecord
  belongs_to :patient
  validates :patient_id, allow_blank: false

Fixed:

class Appointment < ApplicationRecord
  belongs_to :patient
  # rubocop:disable Rails/RedundantPresenceValidationOnBelongsTo
  validates :patient_id, presence: true, allow_blank: false
  # rubocop:enable Rails/RedundantPresenceValidationOnBelongsTo

The code autocorrector does take off the presence validator, and then rails complains about not having a single validator on the line where it validates.


Expected behavior

Does not break existing validate rules.

Actual behavior

Server does not stand up due to error.

Steps to reproduce the problem

bundle exec rubocop -A

RuboCop version

$ bundle exec rubocop --version
1.50.2
@Earlopain
Copy link
Contributor

Earlopain commented Aug 2, 2024

Can you show the error and which version of rails you are using? Your RuboCop version is also somewhat old, please upgrade and do the same for rubocop-rails if applicable.

@wieczorek1990
Copy link
Author

rails: 5.2.3
ruby: 2.6.10

The error message is:

You need to supply at least one validation

Stack trace:

api-1     | /usr/local/bundle/gems/activemodel-5.2.3/lib/active_model/validations/validates.rb:110:in `validates': You need to supply at least one validation (ArgumentError)
api-1     | 	from /mednote-api/app/models/appointment.rb:10:in `<class:Appointment>'
api-1     | 	from /mednote-api/app/models/appointment.rb:1:in `<main>'
api-1     | 	from /usr/local/bundle/gems/bootsnap-1.18.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
api-1     | 	from /usr/local/bundle/gems/bootsnap-1.18.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
api-1     | 	from /usr/local/bundle/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:291:in `block in require'
api-1     | 	from /usr/local/bundle/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:257:in `load_dependency'
api-1     | 	from /usr/local/bundle/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:291:in `require'
api-1     | 	from /usr/local/bundle/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:378:in `block in require_or_load'
api-1     | 	from /usr/local/bundle/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:37:in `block in load_interlock'
api-1     | 	from /usr/local/bundle/gems/activesupport-5.2.3/lib/active_support/dependencies/interlock.rb:14:in `block in loading'
api-1     | 	from /usr/local/bundle/gems/activesupport-5.2.3/lib/active_support/concurrency/share_lock.rb:151:in `exclusive'
api-1     | 	from /usr/local/bundle/gems/activesupport-5.2.3/lib/active_support/dependencies/interlock.rb:13:in `loading'
api-1     | 	from /usr/local/bundle/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:37:in `load_interlock'
api-1     | 	from /usr/local/bundle/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:356:in `require_or_load'
api-1     | 	from /usr/local/bundle/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:334:in `depend_on'
api-1     | 	from /usr/local/bundle/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:246:in `require_dependency'
api-1     | 	from /usr/local/bundle/gems/railties-5.2.3/lib/rails/engine.rb:478:in `block (2 levels) in eager_load!'
api-1     | 	from /usr/local/bundle/gems/railties-5.2.3/lib/rails/engine.rb:477:in `each'
api-1     | 	from /usr/local/bundle/gems/railties-5.2.3/lib/rails/engine.rb:477:in `block in eager_load!'
api-1     | 	from /usr/local/bundle/gems/railties-5.2.3/lib/rails/engine.rb:475:in `each'
api-1     | 	from /usr/local/bundle/gems/railties-5.2.3/lib/rails/engine.rb:475:in `eager_load!'
api-1     | 	from /usr/local/bundle/gems/railties-5.2.3/lib/rails/engine.rb:356:in `eager_load!'
api-1     | 	from /usr/local/bundle/gems/railties-5.2.3/lib/rails/application/finisher.rb:69:in `each'
api-1     | 	from /usr/local/bundle/gems/railties-5.2.3/lib/rails/application/finisher.rb:69:in `block in <module:Finisher>'
api-1     | 	from /usr/local/bundle/gems/railties-5.2.3/lib/rails/initializable.rb:32:in `instance_exec'
api-1     | 	from /usr/local/bundle/gems/railties-5.2.3/lib/rails/initializable.rb:32:in `run'
api-1     | 	from /usr/local/bundle/gems/railties-5.2.3/lib/rails/initializable.rb:61:in `block in run_initializers'
api-1     | 	from /usr/local/lib/ruby/2.6.0/tsort.rb:228:in `block in tsort_each'
api-1     | 	from /usr/local/lib/ruby/2.6.0/tsort.rb:350:in `block (2 levels) in each_strongly_connected_component'
api-1     | 	from /usr/local/lib/ruby/2.6.0/tsort.rb:431:in `each_strongly_connected_component_from'
api-1     | 	from /usr/local/lib/ruby/2.6.0/tsort.rb:349:in `block in each_strongly_connected_component'
api-1     | 	from /usr/local/lib/ruby/2.6.0/tsort.rb:347:in `each'
api-1     | 	from /usr/local/lib/ruby/2.6.0/tsort.rb:347:in `call'
api-1     | 	from /usr/local/lib/ruby/2.6.0/tsort.rb:347:in `each_strongly_connected_component'
api-1     | 	from /usr/local/lib/ruby/2.6.0/tsort.rb:226:in `tsort_each'
api-1     | 	from /usr/local/lib/ruby/2.6.0/tsort.rb:205:in `tsort_each'
api-1     | 	from /usr/local/bundle/gems/railties-5.2.3/lib/rails/initializable.rb:60:in `run_initializers'
api-1     | 	from /usr/local/bundle/gems/railties-5.2.3/lib/rails/application.rb:361:in `initialize!'
api-1     | 	from /mednote-api/config/environment.rb:5:in `<main>'
api-1     | 	from config.ru:3:in `require_relative'
api-1     | 	from config.ru:3:in `block in <main>'
api-1     | 	from /usr/local/bundle/gems/rack-2.2.9/lib/rack/builder.rb:116:in `eval'
api-1     | 	from /usr/local/bundle/gems/rack-2.2.9/lib/rack/builder.rb:116:in `new_from_string'
api-1     | 	from /usr/local/bundle/gems/rack-2.2.9/lib/rack/builder.rb:105:in `load_file'
api-1     | 	from /usr/local/bundle/gems/rack-2.2.9/lib/rack/builder.rb:66:in `parse_file'
api-1     | 	from /usr/local/bundle/gems/rack-2.2.9/lib/rack/server.rb:349:in `build_app_and_options_from_config'
api-1     | 	from /usr/local/bundle/gems/rack-2.2.9/lib/rack/server.rb:249:in `app'
api-1     | 	from /usr/local/bundle/gems/railties-5.2.3/lib/rails/commands/server/server_command.rb:27:in `app'
api-1     | 	from /usr/local/bundle/gems/rack-2.2.9/lib/rack/server.rb:422:in `wrapped_app'
api-1     | 	from /usr/local/bundle/gems/railties-5.2.3/lib/rails/commands/server/server_command.rb:89:in `log_to_stdout'
api-1     | 	from /usr/local/bundle/gems/railties-5.2.3/lib/rails/commands/server/server_command.rb:51:in `start'
api-1     | 	from /usr/local/bundle/gems/railties-5.2.3/lib/rails/commands/server/server_command.rb:147:in `block in perform'
api-1     | 	from /usr/local/bundle/gems/railties-5.2.3/lib/rails/commands/server/server_command.rb:142:in `tap'
api-1     | 	from /usr/local/bundle/gems/railties-5.2.3/lib/rails/commands/server/server_command.rb:142:in `perform'
api-1     | 	from /usr/local/bundle/gems/thor-1.3.1/lib/thor/command.rb:28:in `run'
api-1     | 	from /usr/local/bundle/gems/thor-1.3.1/lib/thor/invocation.rb:127:in `invoke_command'
api-1     | 	from /usr/local/bundle/gems/thor-1.3.1/lib/thor.rb:527:in `dispatch'
api-1     | 	from /usr/local/bundle/gems/railties-5.2.3/lib/rails/command/base.rb:65:in `perform'
api-1     | 	from /usr/local/bundle/gems/railties-5.2.3/lib/rails/command.rb:46:in `invoke'
api-1     | 	from /usr/local/bundle/gems/railties-5.2.3/lib/rails/commands.rb:18:in `<main>'
api-1     | 	from /usr/local/bundle/gems/bootsnap-1.18.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
api-1     | 	from /usr/local/bundle/gems/bootsnap-1.18.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
api-1     | 	from bin/rails:4:in `<main>'
api-1     | => Booting Puma
api-1     | => Rails 5.2.3 application starting in development 
api-1     | => Run `rails server -h` for more startup options
api-1     | Exiting
api-1 exited with code 1

Earlopain added a commit to Earlopain/rubocop-rails that referenced this issue Aug 2, 2024
…nOnBelongsTo`

If presence is the only validation option and other non-validation options
are present, removing it will cause rails to error.
@Earlopain
Copy link
Contributor

Thanks, I thought it had something to do with old rails but it seems that this autocorrect is just invalid code in all versions of it. Fix at #1320

@wieczorek1990
Copy link
Author

wieczorek1990 commented Aug 2, 2024 via email

@koic koic closed this as completed in eae29d8 Aug 2, 2024
koic added a commit that referenced this issue Aug 2, 2024
…nce-validation

[Fix #1319] Fix false positive for `RedundantPresenceValidationOnBelongsTo`
koic pushed a commit to koic/rubocop-rails that referenced this issue Aug 23, 2024
…nOnBelongsTo`

If presence is the only validation option and other non-validation options
are present, removing it will cause rails to error.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants