Emit deprecation warning about inverse_of inference only for valid reflections #51442
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Motivation / Background
This is a small follow-up to #50883.
As we went through all deprecation warnings while we run Rails edge with
we realised that we had warnings for an inferred reflection that would be wrong to infer. Digging deeper into whether this meant that enabling
automatically_invert_plural_associations
would actually mean we'd have completely wrong values in the collection of the inferred association, we realised that no, the reflection was correctly not inferred. However, the warning was emitted regardless.This happens because the deprecation warning is emitted without checking whether the found
reflection
is valid. The warning says that the inverse association could have been automatically inferred but wasn't becauseautomatically_invert_plural_associations
is disabled. However, this is not true, because later on, when we check whether the reflection is valid, we see it's not, and end up returningnil
.Detail
This pull request just adds the
valid_inverse_reflection?(reflection)
condition to determine whether the deprecation warning needs to happen. That already checks thatreflection
is truthy.Checklist
Before submitting the PR make sure the following are checked:
[Fix #issue-number]