-
Notifications
You must be signed in to change notification settings - Fork 21.4k
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
base: main
Are you sure you want to change the base?
Conversation
6bf94e7
to
30c0c53
Compare
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. |
fd5c8e6
to
3c2711c
Compare
@yahonda I modified the class name of the exception has been changed from Also, the PR description has been modified to explain why 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.") |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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! 🙇
3c2711c
to
318d569
Compare
activerecord/test/cases/associations/inverse_associations_test.rb
Outdated
Show resolved
Hide resolved
44306c4
to
f5646df
Compare
ActiveRecord::InverseOfAssociationTableNameMismatchError
if invalid inverse_of is specifiedActiveRecord:: InverseOfAssociationNotFoundError
if invalid inverse_of is specified
1ee8611
to
daffafb
Compare
Thank you for the review 🙏 I have incorporated suggestions and modified the code to use |
Would you address the conflict? |
daffafb
to
850232f
Compare
@yahonda I rebased and force-pushed 🙏 |
194292f
to
b4d16c3
Compare
…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 <[email protected]>
b4d16c3
to
819820d
Compare
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
Additional information
When checking the bi-directionality of inverse_of reflection, it is intuitive to
compare classes. However, the implementation compares
table_name
instead, becauseclass 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:
[Fix #issue-number]