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

Raise ActiveRecord:: InverseOfAssociationNotFoundError if invalid inverse_of is specified #51767

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

Conversation

alpaca-tc
Copy link
Contributor

@alpaca-tc alpaca-tc commented May 8, 2024

Motivation / Background

This error occurs when the association specified in the inverse_of option does not exist on the associated class.
Previously this would be implicitly ignored, so the developer wouldn't know they tried to make an invalid association.

Detail

Post.belongs_to(:user, inverse_of: :comment) # Correct inverse_of is :post
user = User.create!

 # Before:
Post.new(user: user) #=> No error

 # After:
Post.new(user: user)
#=> ActiveRecord::InverseOfAssociationNotFoundError: Could not find the inverse association for user (:comment in User).

Additional information

When checking the bi-directionality of inverse_of reflection, it is intuitive to
compare classes. However, the implementation compares table_name instead, because
class name comparisons are challenging with anonymous classes (such as those used in Rails testing).

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.

@alpaca-tc alpaca-tc force-pushed the assert-inverse-of branch 2 times, most recently from 6bf94e7 to 30c0c53 Compare May 10, 2024 08:41
@yahonda
Copy link
Member

yahonda commented May 16, 2024

Although English isn't my native language, I find the current error message like Inverse association human (:interests in Human) is mismatch table_name." unclear for me, I assume this statement is based on #41552 , where the "recursive" is an adjective, while "mismatch" is a noun.

@alpaca-tc alpaca-tc force-pushed the assert-inverse-of branch 3 times, most recently from fd5c8e6 to 3c2711c Compare May 28, 2024 14:46
@alpaca-tc
Copy link
Contributor Author

@yahonda I modified the class name of the exception has been changed from InverseOfAssociationTableNameMismatchError to InverseOfAssociationNotBidirectionalError.
This is because the old name is not essential, since it expresses implementation details.
In addition, the error message has been changed to point out that it is not bi-directional.

Also, the PR description has been modified to explain why table_name is used for bi-directional comparisons. Originally I said STI considered, but it was correct that it considered the anonymous class.

Please re-review this PR 😄 🙏

if reflection
super("Inverse association #{reflection.name} (#{reflection.inverse_of.name.inspect} in #{reflection.class_name}) is not bi-directional.")
else
super("Inverse association is not bi-directional.")
Copy link
Member

Choose a reason for hiding this comment

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

Even I not sure what this error message means. It seems like it is a requirement for inverse_of to be by directional, but I don't think this is the problem here, is it?

The problem is that the association on the other model doesn't exist, so we should be explicit about that.

Something like:

Inverse association doesn't not exist in the associated class

Copy link
Contributor Author

@alpaca-tc alpaca-tc May 29, 2024

Choose a reason for hiding this comment

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

@rafaelfranca I have corrected the message you commented. 🙏
Should I L80 have also been changed?

Copy link
Member

Choose a reason for hiding this comment

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

Should I L80 have also been changed?

Yes, I made some other comments below too, thanks for your PR! 🙇

activerecord/CHANGELOG.md Outdated Show resolved Hide resolved
activerecord/CHANGELOG.md Outdated Show resolved Hide resolved
activerecord/CHANGELOG.md Outdated Show resolved Hide resolved
@@ -71,6 +71,20 @@ def initialize(reflection = nil)
end
end

class InverseOfAssociationNotBidirectionalError < ActiveRecordError # :nodoc:
Copy link
Member

@zzak zzak May 30, 2024

Choose a reason for hiding this comment

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

I think there was already some discussion about the name, but I prefer InverseOfAssociationNotFoundError, as it's explicit that the "inverse of" doesn't exist.

This matches the style for HasManyThroughAssociationNotFoundError.

@alpaca-tc alpaca-tc changed the title Raise ActiveRecord::InverseOfAssociationTableNameMismatchError if invalid inverse_of is specified Raise ActiveRecord:: InverseOfAssociationNotFoundError if invalid inverse_of is specified May 30, 2024
…verse_of is specified.

This error occurs when the association specified in the `inverse_of` option does not exist on the associated class.
Previously this would be implicitly ignored, so the developer wouldn't know they tried to make an invalid association.

```ruby
Post.belongs_to(:user, inverse_of: :comment) # Correct inverse_of is :post
user = User.create!

 # Before:
Post.new(user: user) #=> No error

 # After:
Post.new(user: user)
 #=> ActiveRecord::InverseOfAssociationNotFoundError: Could not find the inverse association for user (:comment in User).
```

When checking the bi-directionality of inverse_of reflection, it is intuitive to
compare classes. However, the implementation compares table_name instead, because
class name comparisons are challenging with anonymous classes (such as those used in Rails testing).

Co-authored-by: zzak <zzakscott@gmail.com>
@alpaca-tc
Copy link
Contributor Author

Thank you for the review 🙏

I have incorporated suggestions and modified the code to use InverseOfAssociationNotFoundError. The changes have been force-pushed. Please review the updates.

@zzak zzak requested a review from rafaelfranca May 31, 2024 12:48
@zzak zzak added the ready PRs ready to merge label May 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
activerecord ready PRs ready to merge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants