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::InverseOfAssociationTableNameMismatchError 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

If you accidentally have an irrelevant association with inverse_of, it has been implicitly ignored until now. This change causes an exception to be raised if the wrong association is specified.

Detail

Before:

# NOTE: valid inverse_of is `:post`
Post.belongs_to(:user, inverse_of: :comment)
user = User.create!
Post.new(user: user) #=> No error. Ignored invalid inverse_of.

After:

Post.belongs_to(:user, inverse_of: :comment)
user = User.create!
Post.new(user: user) #=> ActiveRecord::InverseOfAssociationTableNameMismatchError: Inverse association user (:comment in User) is mismatch table_name. Expected table_name: 'posts', got 'comments'.

Additional information

In consideration of STI, the wrong relation is checked by comparing table names instead of comparing class.

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.

…nvalid inverse_of is specified

If you accidentally have an irrelevant association with inverse_of, it has been implicitly ignored until now.
This change causes an exception to be raised if the wrong association is specified.

In consideration of STI, the wrong relation is checked by comparing table names instead of comparing class.

Before:
```ruby
Post.belongs_to(:user, inverse_of: :comment)
user = User.create!
Post.new(user: user) #=> No error
```

After:
```ruby
Post.belongs_to(:user, inverse_of: :comment)
user = User.create!
Post.new(user: user) #=> ActiveRecord::InverseOfAssociationTableNameMismatchError: Inverse association user (:comment in User) is mismatch table_name. Expected table_name: 'posts', got 'comments'.
```
@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.

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

2 participants