standardrb / standard-rails

A Standard Ruby plugin that configures rubocop-rails
MIT License
163 stars 15 forks source link

configure InverseOf to ignore scopes #52

Closed Cohen-Carlisle closed 2 months ago

Cohen-Carlisle commented 2 months ago

scope's inverses are automatically inferred with rails 7+

closes #51

I manually tested this locally and it resolved my issue. (Which means I managed not to typo IgnoreScopes: true :smile:)

searls commented 2 months ago

For my own understanding, can you show me a situation for which the rule is still needed? When I read about the Rails 7 changes, I got the impression that all of this is handled automatically now

Cohen-Carlisle commented 2 months ago

My understanding is that there are still conditions where you need to supply inverse_of, like if you use a custom foreign key column name or a custom relation name (book.author joining to the Person table). I believe it was also mentioned on the Rails PR that implemented automatic_scope_inversing that if there is a scope/proc on both sides of the relation, it can’t (necessarily?) be automatically inferred.

I can test these out when I’m back home this evening (US Eastern).

Cohen-Carlisle commented 2 months ago

Just did some testing and https://guides.rubyonrails.org/association_basics.html#bi-directional-associations is correct that you need to specify inverse_of on the has_many side of a relation when using the foreign_key option on the belongs_to side. Interestingly, rubocop gets this backwards and tells you to add inverse_of on the belongs_to side.

Edit: I found this rubocop-rails issue talking about this problem. Should we lean towards disabling this altogether if it's going to potentially spit out false positives?

searls commented 2 months ago

This is the second conversation that's made me feel that this cop might be doing the wrong thing. @koic?

Cohen-Carlisle commented 2 months ago

@searls how would you like to proceed?

Cohen-Carlisle commented 2 months ago

superseded by #54