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
Do not consolidate persisted in-memory records #43517
Conversation
82c82cd
to
ca03f34
Compare
Could you add a test and a changelog entry? |
@ghiculescu sure thing! |
ca03f34
to
ab2e86b
Compare
@ghiculescu I've now added a test and a changelog entry as requested. 🙂 |
ab2e86b
to
2367d38
Compare
def test_target_merging_ignores_persisted_in_memory_records | ||
david = authors(:david) | ||
assert david.thinking_posts.include?(posts(:thinking)) | ||
|
||
david.thinking_posts.create!(title: "Something else entirely", body: "Does not matter.") | ||
|
||
assert_equal 1, david.thinking_posts.size | ||
assert_equal 1, david.thinking_posts.to_a.size | ||
end |
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.
def test_target_merging_ignores_persisted_in_memory_records | |
david = authors(:david) | |
assert david.thinking_posts.include?(posts(:thinking)) | |
david.thinking_posts.create!(title: "Something else entirely", body: "Does not matter.") | |
assert_equal 1, david.thinking_posts.size | |
assert_equal 1, david.thinking_posts.to_a.size | |
end | |
def test_target_merging_ignores_persisted_in_memory_records | |
david = authors(:david) | |
assert david.thinking_posts.include?(posts(:thinking)) | |
david.thinking_posts.create!(title: "Something else entirely", body: "Does not matter.") | |
assert_equal 1, david.thinking_posts.size | |
assert_equal 1, david.thinking_posts.to_a.size | |
end | |
def test_target_merging_does_not_ignores_persisted_in_memory_records_that_match_scope | |
david = authors(:david) | |
assert david.thinking_posts.include?(posts(:thinking)) | |
david.thinking_posts.create!(title: "So I was thinking", body: "This should be included in the scope.") | |
assert_equal 2, david.thinking_posts.size | |
assert_equal 2, david.thinking_posts.to_a.size | |
end |
Does this also work? (Mostly just want to make sure I've understood the issue correctly.)
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.
Yes, correct. That test case would pass as well. 🙂
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.
Is it worth adding it as an additional test?
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.
I don't think so. That behaviour was already present prior to my change, so I would expect things to blow up left and right if that part broke. But if anyone feel strongly about it, I don't mind adding another test case.
@@ -335,7 +335,7 @@ def merge_target_lists(persisted, memory) | |||
end | |||
end | |||
|
|||
persisted + memory | |||
persisted + memory.reject(&:persisted?) |
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.
If the issue is that persisted
and memory
have the same objects, is this a better fix?
persisted + memory.reject(&:persisted?) | |
[persisted + memory].flatten.uniq |
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.
why don't just do
(persisted + memory).uniq
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.
🤦 yeah that
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.
The issue isn't that persisted
and memory
contains the same records. In fact, that is to be expected. merge_target_lists
already handles duplicates. The issue is that the memory
collection may contain persisted records that are not present in the persisted
collection. These records are currently being included even though they shouldn't. The persisted
collection is already up-to-date, so if there are records in memory that are persisted but not part of persisted
they are invalid (e.g., violating the association scope).
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.
@kevinsjoberg I think the documentation at the beginning of this method should also be updated to reflect this change.
@kevinsjoberg I feel like the root cause is the record being added to I think it's this line:
|
I was thinking the same thing initially but came to the conclusion that it can't be fixed at that point. I might be wrong here, but to my understanding scopes are not enforced at that level. |
`merge_target_lists` is called with two arguments. A collection of persisted records and a collection of in-memory records. If the in-memory collection contains any persisted records we can safely reject them as the `persisted` collection is already up-to-date.
2367d38
to
4d5aed9
Compare
I've rebased with latest |
Summary
merge_target_lists
is called with two arguments. A collection ofpersisted records and a collection of in-memory records. If the
in-memory collection contains any persisted records we can safely
reject them as the
persisted
collection is already up-to-date.Fixes #43516.