Fix child record's validation clearing the parent's errors (has_one/belongs_to) #54417
+21
−4
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Motivation / Background
This Pull Request has been created because I wanted to try fixing a bug, specifically FIXES #54367
I don't have a particular need for the bug to be fixed, I'm just trying to get more familiar with Rails.
Detail
This Pull Request changes the validation logic on
has_one/belongs_to
associations to allow validation ofhas_one
's inverse while thebelongs_to
record is being validated.Let
pirate
be thehas_one
side andship
be thebelongs_to
side. Ship's attributes have errors, pirate's do not.Step 1:
pirate.valid?
-> pirate is validated -> pirate validates ship -> pirate now has ship's nested errorFrom my understanding, the way this worked before this PR was:
Step 2:
ship.valid?
With this change:
Step 2:
ship.valid?
:name
ship.name
pirate.ship.name
This seems to be consistent with 7.2.2. I confirmed manually by using the gist on the issue and changing the test:
The outcome is the same on my branch and on 7.2.2. If I should make an attempt to avoid the double validation of the
belongs_to
side, please let me know and I will try to make the change.Additional information
Please see the gist posted on the issue.
This is my first time reading or modifying significant (and it was significant 😩) amounts of Rails source code. It's also my first PR here, hopefully of many to come.
That said please don't feel the need to be pleasant, I'm willing to put in time to get this done right. I don't take offense at being told my code is bad or that I should make more changes, be as rude as you want.
Checklist
Before submitting the PR make sure the following are checked:
[Fix #issue-number]