-
-
Notifications
You must be signed in to change notification settings - Fork 270
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
Cop Idea: DuplicateAssociation
cop
#599
Comments
DuplicateAssociation
copDuplicateAssociation
cop
I expect this should also cover cases where the definitions refer to the same field but aren't identical, e.g.: belongs_to :foo
belongs_to :foo, required: false |
Wondering how it works. Maybe duplicate declarations complement each other, like e.g. new-style validations do? belongs_to :user, -> { joins(:friends) }
belongs_to :user, foreign_key: :account_id, class_name: 'Account'
belongs_to :user, counter_cache: true
belongs_to :user, touch: :employees_last_updated_at If not, why Rails doesn't warn of a duplicate declaration? Maybe it's like that to be able to group common options? belongs_to :user, foreign_key: :account_id, class_name: 'Account'
with_options counter_cache: true do
belongs_to :user
belongs_to :book
end What if It's fine to tag identical declarations, though. Unless one of them is scoped inside |
Yeah, multiple I could see this sort of thing actually just being a Rails PR, to error on association redefinition. But considering a scope redefinition is just a log line, they might not be up for an error. There's certainly a lot of cases that a cop couldn't cover. It wouldn't be able to tell if you're redefining an association that's already defined in a parent class or a concern, for example. But it would help with for the same file at least. |
Apparently, the last declaration wins. This is quite unfortunate due to the case of Indeed, defining the same association twice in the same scope is a mistake, as the first declaration will be disregarded, and it's at best a redundant statement. Including the case when one of them is inside Would you like to contribute a cop for it provided all necessary guidance? ASSOCIATIONS = Set.new(%i[belongs_to has_one has_many has_and_belongs_to_many])
def_node_matcher :association?, <<~PATTERN
(send nil? $#ASSOCIATIONS $_ ...)
PATTERN Then in the cop: def on_class(node)
associations = node.children.map { |child| association?(node) }
# reject [nil, nil] ones
# => [[:has_many, :books], [:belongs_to, :city]]
# now, find duplicates and add an offence:
add_offense(node) do |corrector|
# remove duplicates except for the last one
end
end The case with |
[Fix #599] Add Rails/DuplicateAssociation cop
Recently we had a few PRs created that defined the same association. They were both in code review at the same time, so both added the association that did not previously exist. When both got merged, we didn't realize that we now had the same association defined twice in 1 model. It would be nice if there were a cop that caught this and pointed it out.
For example:
The text was updated successfully, but these errors were encountered: