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/DuplicateAssociation: False positive for belongs_to #1168

Closed
franzliedke opened this issue Oct 27, 2023 · 3 comments
Closed

Rails/DuplicateAssociation: False positive for belongs_to #1168

franzliedke opened this issue Oct 27, 2023 · 3 comments
Labels
bug Something isn't working

Comments

@franzliedke
Copy link

Introduced by PR #1145.

The documented "bad" belongs_to example is a false positive.

The following example should make this clear:

class GraphEdge
  belongs_to :source_node, class_name: "GraphNode"
  belongs_to :target_node, class_name: "GraphNode"
end

Contrary to has_many, the names of belongs_to associations are not arbitrary, as they implicitly reference the foreign key.

Originally posted by @franzliedke in #1145 (comment)

@twohlix
Copy link

twohlix commented Oct 27, 2023

This is a common use case in one of my codebases (and new false positive for the cop) where you have two or three users joined in context specific ways for a transaction. e.g.

  belongs_to :attendant, class_name: 'User'
  belongs_to :collector, class_name: 'User'

I'm in agreement that in my codebase its all belongs_to relationships.

@gsar
Copy link

gsar commented Oct 28, 2023

what's more, the autocorrect happily deletes the second line when asking for --safe-auto-correct. this autocorrect should not be marked safe, i.e. should only work with -A

@koic koic added the bug Something isn't working label Oct 28, 2023
@koic
Copy link
Member

koic commented Oct 28, 2023

This issue was resolved by #1170 and RuboCop Rails 2.22.1 was released. Thank you for the feedback!

@koic koic closed this as completed Oct 28, 2023
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

4 participants