Skip to content

Commit 060ae8c

Browse files
committed
Merge PR rails#43517
2 parents 7a95a73 + 4d5aed9 commit 060ae8c

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

activerecord/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* Ignore persisted in-memory records when merging target lists.
2+
3+
*Kevin Sjöberg*
4+
15
* Add a new option `:update_only` to `upsert_all` to configure the list of columns to update in case of conflict.
26

37
Before, you could only customize the update SQL sentence via `:on_duplicate`. There is now a new option `:update_only` that lets you provide a list of columns to update in case of conflict:

activerecord/lib/active_record/associations/collection_association.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ def merge_target_lists(persisted, memory)
335335
end
336336
end
337337

338-
persisted + memory
338+
persisted + memory.reject(&:persisted?)
339339
end
340340

341341
def _create_record(attributes, raise = false, &block)

activerecord/test/cases/associations_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,16 @@ def test_reset_unloads_target
289289
assert_not_predicate david.posts, :loaded?
290290
assert_not_predicate david.posts, :loaded
291291
end
292+
293+
def test_target_merging_ignores_persisted_in_memory_records
294+
david = authors(:david)
295+
assert david.thinking_posts.include?(posts(:thinking))
296+
297+
david.thinking_posts.create!(title: "Something else entirely", body: "Does not matter.")
298+
299+
assert_equal 1, david.thinking_posts.size
300+
assert_equal 1, david.thinking_posts.to_a.size
301+
end
292302
end
293303

294304
class OverridingAssociationsTest < ActiveRecord::TestCase

0 commit comments

Comments
 (0)