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
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/reflection.rb
Expand Up @@ -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 🤔

ActiveRecord.deprecator.warn(
"The `#{active_record.name}##{name}` inverse association could have been automatically" \
" inferred as `#{klass.name}##{plural_inverse_name}` but wasn't because `automatically_invert_plural_associations`" \
Expand Down