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

Emit deprecation warning about inverse_of inference only for valid reflections #51442

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

rosa
Copy link
Member

@rosa rosa commented Mar 28, 2024

Motivation / Background

This is a small follow-up to #50883.

As we went through all deprecation warnings while we run Rails edge with

config.active_record.automatically_invert_plural_associations = false

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 because automatically_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 returning nil.

Detail

This pull request just adds the valid_inverse_reflection?(reflection) condition to determine whether the deprecation warning needs to happen. That already checks that reflection is truthy.

Checklist

Before submitting the PR make sure the following are checked:

  • This Pull Request is related to one change. Unrelated changes should be opened in separate PRs.
  • Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: [Fix #issue-number]
  • Tests are added or updated if you fix a bug or add a feature.
  • CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.

…flections

That is, if the reflection found is not valid, we don't want to emit the warning
because it's misleading. It says the inverse association could have been automatically
inferred but wasn't because `automatically_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 returning `nil`.
@@ -708,7 +708,7 @@ def automatic_inverse_of
plural_inverse_name = ActiveSupport::Inflector.pluralize(inverse_name)
reflection = klass._reflect_on_association(plural_inverse_name)

if reflection && !active_record.automatically_invert_plural_associations
if valid_inverse_reflection?(reflection) && !active_record.automatically_invert_plural_associations
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

valid_inverse_reflection calls .name on its argument, but given this branch had a truthiness check, I assume that could blow up if reflection is falsey.

We should probably either keep the truthiness check first, or use &.name in valid_inverse_reflection?.

It would also be good to have test coverage for this, to be sure it behaves as expected.

Copy link
Member Author

@rosa rosa Mar 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for reviewing! 🙏🏻

valid_inverse_reflection calls .name on its argument, but given this branch had a truthiness check, I assume that could blow up if reflection is falsey.

We should probably either keep the truthiness check first, or use &.name in valid_inverse_reflection?.

valid_inverse_reflection? has a truthiness check first thing:

def valid_inverse_reflection?(reflection)
reflection &&

That's why I just replaced the existing truthiness check, as I mentioned in the PR description, in the Detail section.

It would also be good to have test coverage for this, to be sure it behaves as expected.

The original PR that introduced this new configuration flag and deprecation warning didn't add any tests, and this is a fairly tiny change over it 🤔

@skipkayhil skipkayhil added this to the 7.2.0 milestone Apr 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants