Skip to content

Commit 0f43592

Browse files
committed
Fix CollectionAssociation#replace to return new target (closes rails#6231)
Conflicts: activerecord/test/cases/associations/has_many_associations_test.rb
1 parent e3033e3 commit 0f43592

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

activerecord/lib/active_record/associations/collection_association.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,8 @@ def replace_records(new_target, original_target)
481481
raise RecordNotSaved, "Failed to replace #{reflection.name} because one or more of the " \
482482
"new records could not be saved."
483483
end
484+
485+
new_target
484486
end
485487

486488
def concat_records(records)

activerecord/test/cases/associations/has_many_associations_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1686,4 +1686,16 @@ def test_replace
16861686
assert_equal [bulb2], car.bulbs
16871687
assert_equal [bulb2], car.reload.bulbs
16881688
end
1689+
1690+
def test_replace_returns_new_target
1691+
car = Car.create(:name => 'honda')
1692+
bulb1 = car.bulbs.create
1693+
bulb2 = car.bulbs.create
1694+
bulb3 = Bulb.create
1695+
1696+
assert_equal [bulb1, bulb2], car.bulbs
1697+
result = car.bulbs.replace([bulb1, bulb3])
1698+
assert_equal [bulb1, bulb3], car.bulbs
1699+
assert_equal [bulb1, bulb3], result
1700+
end
16891701
end

0 commit comments

Comments
 (0)