You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is your feature request related to a problem? Please describe.
The Rails/InverseOf cop reports violations for any belongs_to associations with options like :foreign_key that prevent automatic inverse detection. This is great for has_one/belongs_to association pairs, and for belongs_to associations on join models in the middle of a has_many :through, but I don't think it's that helpful for the common has_many/belongs_to pairs.
Rails will automatically infer the inverse of the :comments association, but it does NOT infer the inverse of the :post association (Rails can only automatically detect singular inverses, so we'd get the automatic inverse if the other side was has_one :comment, but not for the collection has_many :comments).
In general I don't think having an inverse on the belongs_to side of a has_many/belongs_to is helpful. For example doing comment.post.comments will still require a load of all the comments (whereas going the other way a missing inverse on the has_many could cause N+1 queries with something like post.comments.each(&:post)).
If we add the :foreign_key option to the belongs_to:
Rails no longer automatically infers the inverse for either association. The Rails/InverseOf cop tells us to add an :inverse_of to the belongs_to association, but I don't think that's actually helpful. What we really want is to add :inverse_of to has_many :comments, but I'm not sure there's a good way for this cop to tell us that (and most of the time the :foreign_key option would be on both associations anyway).
I'm trying to introduce the Rails/InverseOf cop at GitHub, but I'd rather not force people to add inverses in places where it's not helpful. That would seem to add friction without much benefit.
Describe the solution you'd like
I'd like an option to disable linting belongs_to associations and focus only on has_one and has_many (and I'd be happy to submit a PR for this if it is acceptable). This does mean we'll miss some cases where having inverse_of on the belongs_to might have been helpful (i.e. when it's the other side of a has_one or it's in a join model), but those cases are less common than has_many/belongs_to pairs and at least in my case that tradeoff is worth it.
Describe alternatives you've considered
I've been working to get Rails to automatically infer inverses in more places, essentially to make this cop unnecessary. I managed to get Rails to infer inverses when there are scopes, but trying to get it to infer inverses more broadly has stalled because the solution is too complex can impact performance.
The text was updated successfully, but these errors were encountered:
No, that one had to do with a new scope option added in Rails. This one has to do with disabling the cop for belongs_to associations entirely.
But given that this issue hasn't seen any movement in a year, I think it's fine to close it for now (we can always reopen if there is renewed interest).
Is your feature request related to a problem? Please describe.
The Rails/InverseOf cop reports violations for any
belongs_to
associations with options like:foreign_key
that prevent automatic inverse detection. This is great forhas_one
/belongs_to
association pairs, and forbelongs_to
associations on join models in the middle of ahas_many :through
, but I don't think it's that helpful for the commonhas_many
/belongs_to
pairs.Consider the following example:
Rails will automatically infer the inverse of the
:comments
association, but it does NOT infer the inverse of the:post
association (Rails can only automatically detect singular inverses, so we'd get the automatic inverse if the other side washas_one :comment
, but not for the collectionhas_many :comments
).In general I don't think having an inverse on the
belongs_to
side of ahas_many
/belongs_to
is helpful. For example doingcomment.post.comments
will still require a load of all the comments (whereas going the other way a missing inverse on thehas_many
could cause N+1 queries with something likepost.comments.each(&:post)
).If we add the
:foreign_key
option to thebelongs_to
:Rails no longer automatically infers the inverse for either association. The Rails/InverseOf cop tells us to add an
:inverse_of
to thebelongs_to
association, but I don't think that's actually helpful. What we really want is to add:inverse_of
tohas_many :comments
, but I'm not sure there's a good way for this cop to tell us that (and most of the time the:foreign_key
option would be on both associations anyway).I'm trying to introduce the Rails/InverseOf cop at GitHub, but I'd rather not force people to add inverses in places where it's not helpful. That would seem to add friction without much benefit.
Describe the solution you'd like
I'd like an option to disable linting
belongs_to
associations and focus only onhas_one
andhas_many
(and I'd be happy to submit a PR for this if it is acceptable). This does mean we'll miss some cases where havinginverse_of
on thebelongs_to
might have been helpful (i.e. when it's the other side of ahas_one
or it's in a join model), but those cases are less common thanhas_many
/belongs_to
pairs and at least in my case that tradeoff is worth it.Describe alternatives you've considered
I've been working to get Rails to automatically infer inverses in more places, essentially to make this cop unnecessary. I managed to get Rails to infer inverses when there are scopes, but trying to get it to infer inverses more broadly has stalled because the solution is too complex can impact performance.
The text was updated successfully, but these errors were encountered: