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
Infer foreign_key
when inverse_of
is present
#47797
Conversation
Can you write a test for this? This code doesn't break any existing test, but nothing prevents this code to be removed in he future, so we need a test to make sure the feature you are adding will not be removed. Also, this needs a changelog since it is a new feature. It is small, but it is still a feature. |
@rafaelfranca I updated some of the existing models to use the new behaviour so some various old test now explode when the new behaviour is reverted ( Is that sufficient? It was quick and I figure it is simpler than adding new associations that rely on the new inference, as I am not sure what the conventions are for doing that. |
@@ -4,7 +4,7 @@ class Book < ActiveRecord::Base | |||
belongs_to :author | |||
belongs_to :format_record, polymorphic: true | |||
|
|||
has_many :citations, foreign_key: "book1_id", inverse_of: :book |
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.
ha! good idea
I'm not sure whats causing the markdown lint failure, the change I made seems to obey the rules the error is complaining about. |
Ah, probably just bad timing. #47779 |
Whoops, yeah that must have snuck in. Fixed by #47812 if you want to rebase. 🙏 Sorry for the trouble! |
Automatically infer `foreign_key` on `has_one` and `has_many` associations when `inverse_of` is present. When inverse of is present, rails has all the info it needs to figure out what the foreign_key on the associated model should be. I can't imagine this breaking anything
Co-authored-by: Rafael Mendonça França <rafael@franca.dev>
I think this is good to go, the build failure seems unrelated (the failing test |
@@ -1,3 +1,19 @@ | |||
* Infer `foerign_key` when `inverse_of` is present on `has_one` and `has_many` associations. |
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.
misspelling
foerign_key
when inverse_of
is presentforeign_key
when inverse_of
is present
Motivation / Background
This Pull Request makes the behaviour of
has_one
andhas_many
associations more convenient. This new behaviour lines up with what I thought rails did based on the docs, as it is not explicit about if theinverse_of
option depends on theforeign_key
option.This makes
has_one
andhas_many
associations automatically infer theforeign_key
option when theinverse_of
option is present.Detail
When
inverse_of
is present, rails has all the info it needs to figure out what theforeign_key
on the associated model should be. I can't imagine this breaking anything as it doesn't change the default, and anywhere where this would change the inferredforeign_key
it would've been broken otherwise I think.Checklist
Before submitting the PR make sure the following are checked:
[Fix #issue-number]