rails
rails copied to clipboard
Fix accidental building of child records when `inverse_of` is specified
Summary
If an existing record with inverse_of children was duplicated, additional child records would be built. These new records could be persisted upon save or update inadvertently. I added a method to address that case and prevent the accidental creation of additional associations.
I traced the problem back to this line in the replace_on_target
method in CollectionAssociation
. Using the steps to reproduce the problem in the original issue as an example, I wrote a test to cover the case of new child records being added to existing owner records when inversing is in play.
With this change in place, the original steps to reproduce the problem deliver the expected result of no additional records.
Fixes #44819.
I force-pushed an update here to resolve an activerecord/CHANGELOG.md
conflict.
@ghiculescu, since it's been a while I'm giving this a little nudge. I'm happy to revise or close this pull request if this approach isn't preferred.
Thanks @clouvet, but this would need review from a member of the Rails core / committers teams. I think the approach is fine. They are pretty busy, but they will eventually get to it :)
It looks like you have a merge conflict btw.
Thanks, @ghiculescu, I appreciate the response. I resolved the conflict with activerecord/CHANGELOG.md
again.
@ghiculescu I made changes here in response to your review: I dropped the CHANGELOG addition, accepted your suggestion to use unless
instead of if !
, and revised the comment on new_record_inverse_of_existing_owner?
to explain why that new method exists.
I'm happy to make additional changes if necessary, and of course I'll squash these multiple commits down to one when the time comes.
Thanks for the review!
Sorry for the noise here. I squashed the cleanup commits to make this pull request suitable for merging.