Skip to content

Commit

Permalink
Do not consolidate persisted in-memory records
Browse files Browse the repository at this point in the history
`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.
  • Loading branch information
hovsater committed Nov 3, 2021
1 parent 57fe7df commit 4d5aed9
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
4 changes: 4 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
* Ignore persisted in-memory records when merging target lists.

*Kevin Sjöberg*

* Add nested_attributes_for support for `delegated_type`

```ruby
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ def merge_target_lists(persisted, memory)
end
end

persisted + memory
persisted + memory.reject(&:persisted?)
end

def _create_record(attributes, raise = false, &block)
Expand Down
10 changes: 10 additions & 0 deletions activerecord/test/cases/associations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,16 @@ def test_reset_unloads_target
assert_not_predicate david.posts, :loaded?
assert_not_predicate david.posts, :loaded
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
end

class OverridingAssociationsTest < ActiveRecord::TestCase
Expand Down

0 comments on commit 4d5aed9

Please sign in to comment.