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

Rubocop Missing Offence for Identical has_many Definitions #1128

Closed
Ivanov-Anton opened this issue Sep 20, 2023 · 0 comments · Fixed by #1145
Closed

Rubocop Missing Offence for Identical has_many Definitions #1128

Ivanov-Anton opened this issue Sep 20, 2023 · 0 comments · Fixed by #1145
Labels
enhancement New feature or request

Comments

@Ivanov-Anton
Copy link

Ivanov-Anton commented Sep 20, 2023

I have identified a potential issue in the behavior of Rubocop regarding the detection of identical has_many definitions in an ActiveRecord model. When two has_many associations have different names but share identical options, such as class_name, Rubocop fails to raise an offense, which it should.

in rails, there is one exception when has_many method has lambda then Rubocop should NOT add offense see example has_many :comments, -> { pending }, class_name: 'Main::Comment'

Steps to Reproduce:

Create an ActiveRecord model with two has_many associations using different names but sharing identical options, such as class_name.

Example:


has_many :comments, class_name: 'Main::Comment'
...
has_many :commentaries, class_name: 'Main::Comment'

Expected Result:

Rubocop should raise an offense indicating that there are identical has_many definitions with the same options.

Actual Result:

Rubocop does not raise any offense for these identical has_many definitions, even though they should be detected as a potential issue.

Additional info:

rubocop-rails (2.21.1)
cop name: Rails/DuplicateAssociation

@koic koic added bug Something isn't working enhancement New feature or request and removed bug Something isn't working labels Oct 5, 2023
koic added a commit to koic/rubocop-rails that referenced this issue Oct 6, 2023
…te `class_name`

Fixes rubocop#1128.

This PR makes `Rails/DuplicateAssociation` aware of duplicate `class_name`.
koic added a commit that referenced this issue Oct 10, 2023
…ware_of_duplicate_class_name

[Fix #1128] Make `Rails/DuplicateAssociation` aware of duplicate `class_name`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants