Skip to content

Commit d641646

Browse files
committed
Ensure that CollectionAssociation#replace returns proper target
The fix commited in e2a070c was returning the `new_target`, as a try to return whatever user replaced association with. The problem is, the resulting association target may be ordered differently. In such case we want to return the target that will be later used for that association.
1 parent 4dd4621 commit d641646

File tree

2 files changed

+3
-3
lines changed

2 files changed

+3
-3
lines changed

activerecord/lib/active_record/associations/collection_association.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ def replace_records(new_target, original_target)
492492
"new records could not be saved."
493493
end
494494

495-
new_target
495+
target
496496
end
497497

498498
def concat_records(records)

activerecord/test/cases/associations/has_many_associations_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1687,14 +1687,14 @@ def test_replace
16871687
assert_equal [bulb2], car.reload.bulbs
16881688
end
16891689

1690-
def test_replace_returns_new_target
1690+
def test_replace_returns_target
16911691
car = Car.create(:name => 'honda')
16921692
bulb1 = car.bulbs.create
16931693
bulb2 = car.bulbs.create
16941694
bulb3 = Bulb.create
16951695

16961696
assert_equal [bulb1, bulb2], car.bulbs
1697-
result = car.bulbs.replace([bulb1, bulb3])
1697+
result = car.bulbs.replace([bulb3, bulb1])
16981698
assert_equal [bulb1, bulb3], car.bulbs
16991699
assert_equal [bulb1, bulb3], result
17001700
end

0 commit comments

Comments
 (0)