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

configure InverseOf to ignore scopes #52

Conversation

Cohen-Carlisle
Copy link
Contributor

@Cohen-Carlisle Cohen-Carlisle commented Jul 11, 2024

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 😄)

scope's inverses are automatically inferred with rails 7+
@searls
Copy link
Contributor

searls commented Jul 12, 2024

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
Copy link
Contributor Author

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
Copy link
Contributor Author

Cohen-Carlisle commented Jul 12, 2024

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
Copy link
Contributor

searls commented Jul 13, 2024

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

@Cohen-Carlisle
Copy link
Contributor Author

@searls how would you like to proceed?

Cohen-Carlisle added a commit to Cohen-Carlisle/standard-rails that referenced this pull request Jul 25, 2024
see standardrb#51
and standardrb#52
for some details about why this cop can be problematic.
TLDR: it currently flags associations with scopes even though
Rails 7+ handles them automatically. it also flags the less
problematic side of has_many associations and fails to stop
N+1 queries via its suggested fix.
@Cohen-Carlisle
Copy link
Contributor Author

superseded by #54

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use option IgnoreScopes: true for Rails/InverseOf
2 participants