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 doesn't work on some associations #792

Closed
vlad-pisanov opened this issue Sep 22, 2022 · 1 comment
Labels
bug Something isn't working

Comments

@vlad-pisanov
Copy link
Contributor

Consider

class User1 < ApplicationRecord
  belongs_to :company, -> { where(foo: true) }
  validates :company_id, presence: true
end

class User2 < ApplicationRecord
  belongs_to :company, inverse_of: :employee
  validates :company_id, presence: true
end

class User3 < ApplicationRecord
  belongs_to :company, -> { where(foo: true) }, inverse_of: :employee
  validates :company_id, presence: true
end

Rails/RedundantPresenceValidationOnBelongsTo flags User1 and User2 as offenses, but not User3.

Expected Behavior

All three examples should be flagged as offenses as they perform redundant presence validations on company_id

RuboCop version

$ [bundle exec] rubocop -V
1.36.0 (using Parser 3.1.2.1, rubocop-ast 1.21.0, running on ruby 2.6.9) [x86_64-darwin21]
  - rubocop-minitest 0.22.1
  - rubocop-performance 1.15.0
  - rubocop-rails 2.16.1
@koic koic added the bug Something isn't working label Sep 23, 2022
@PedroAugustoRamalhoDuarte
Copy link
Contributor

Strange, when we use company instead company_id, it works:

class User3 < ApplicationRecord
  belongs_to :company, -> { where(foo: true) }, inverse_of: :employee
  validates :company, presence: true
end

I can try to fix this one

PedroAugustoRamalhoDuarte added a commit to PedroAugustoRamalhoDuarte/rubocop-rails that referenced this issue Oct 16, 2022
PedroAugustoRamalhoDuarte added a commit to PedroAugustoRamalhoDuarte/rubocop-rails that referenced this issue Oct 16, 2022
@koic koic closed this as completed in 2e22634 Oct 19, 2022
koic added a commit that referenced this issue Oct 19, 2022
…e_negative_for_rails_redundant_presence_validation_on_belongs_to

[Fix #792] Fixes a false negative for Rails/RedundantPresenceValidationOnBelongsTo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants