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

Improve singular association creation #46386

Merged
merged 1 commit into from Nov 1, 2022

Conversation

lazaronixon
Copy link
Contributor

@lazaronixon lazaronixon commented Oct 30, 2022

Motivation / Background

Replacing a has_one association nowadays is very confusing...

  • create_association calls INSERT then DELETE in different transactions whilerecord.assoctiation = new_record calls DELETE then INSERT in a single transaction.
  • create_association raises an exception if your association is backed by a unique index, since it first try to create the new record before delete the old one.
  • create_association is prone to create duplicated rows if your association is not backed by a unique index, even though it has a logic to delete the old record later.

Fixes #45920

Detail

This pull request improves the create_association method to work similarly to record.assoctiation = new_record, so when replacing an association, a single transaction is created and the old record is deleted before a new one is created, making the method safer with a unique index or not and less confusing.

Before:

# begin transaction
INSERT new_record;
# commit transaction

# begin transaction
DELETE old_record;
# commit transaction

After:

# begin transaction
DELETE old_record;
INSERT new_record;
# commit transaction

Additional information

https://andycroll.com/ruby/be-careful-assigning-to-has-one-relations

Checklist

Before submitting the PR make sure the following are checked:

  • This Pull Request is related to one change. Changes that are unrelated 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.
  • CI is passing.

@lazaronixon lazaronixon force-pushed the fix-has-one-create-record branch 3 times, most recently from 0669ddd to 0af27c4 Compare October 30, 2022 07:35
Now when you have a has_one association and create_association is 
called:

```
# begin transaction
DELETE old record
INSERT new record
# commit transaction
```
@byroot byroot merged commit d2cb5b7 into rails:main Nov 1, 2022
zzak added a commit to zzak/rails that referenced this pull request Jul 26, 2023
…reate-record"

This reverts commit d2cb5b7, reversing
changes made to 348e609.
paulreece pushed a commit to paulreece/rails-paulreece that referenced this pull request Aug 26, 2023
…reate-record"

This reverts commit d2cb5b7, reversing
changes made to 348e609.
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.

Replace has_one through create_association might create duplicated rows
2 participants