From 0f4359252fa08dfc30de21aca329cb27eddfaee5 Mon Sep 17 00:00:00 2001 From: Piotr Sarnacki Date: Wed, 9 May 2012 19:35:34 -0700 Subject: [PATCH] Fix CollectionAssociation#replace to return new target (closes #6231) Conflicts: activerecord/test/cases/associations/has_many_associations_test.rb --- .../associations/collection_association.rb | 2 ++ .../cases/associations/has_many_associations_test.rb | 12 ++++++++++++ 2 files changed, 14 insertions(+) diff --git a/activerecord/lib/active_record/associations/collection_association.rb b/activerecord/lib/active_record/associations/collection_association.rb index 0f32ce7bd4be0..3e853454a84e1 100644 --- a/activerecord/lib/active_record/associations/collection_association.rb +++ b/activerecord/lib/active_record/associations/collection_association.rb @@ -481,6 +481,8 @@ def replace_records(new_target, original_target) raise RecordNotSaved, "Failed to replace #{reflection.name} because one or more of the " \ "new records could not be saved." end + + new_target end def concat_records(records) diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index f7b2b42959e43..a28b8e6651a1c 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -1686,4 +1686,16 @@ def test_replace assert_equal [bulb2], car.bulbs assert_equal [bulb2], car.reload.bulbs end + + def test_replace_returns_new_target + car = Car.create(:name => 'honda') + bulb1 = car.bulbs.create + bulb2 = car.bulbs.create + bulb3 = Bulb.create + + assert_equal [bulb1, bulb2], car.bulbs + result = car.bulbs.replace([bulb1, bulb3]) + assert_equal [bulb1, bulb3], car.bulbs + assert_equal [bulb1, bulb3], result + end end