aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPiotr Sarnacki <drogus@gmail.com>2012-05-09 19:35:34 -0700
committerPiotr Sarnacki <drogus@gmail.com>2012-05-16 17:22:44 -0700
commite2a070cf47fb9a845ecf53ff2f437390598fe0b7 (patch)
tree584b82c8f513676baa6cc134234eb1cfb4fd431d
parent1f0e2de8670587a75453f064cf384c2382c88526 (diff)
downloadrails-e2a070cf47fb9a845ecf53ff2f437390598fe0b7.tar.gz
rails-e2a070cf47fb9a845ecf53ff2f437390598fe0b7.tar.bz2
rails-e2a070cf47fb9a845ecf53ff2f437390598fe0b7.zip
Fix CollectionAssociation#replace to return new target (closes #6231)
-rw-r--r--activerecord/lib/active_record/associations/collection_association.rb2
-rw-r--r--activerecord/test/cases/associations/has_many_associations_test.rb12
2 files changed, 14 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/associations/collection_association.rb b/activerecord/lib/active_record/associations/collection_association.rb
index 00321ec860..56ec4c5c23 100644
--- a/activerecord/lib/active_record/associations/collection_association.rb
+++ b/activerecord/lib/active_record/associations/collection_association.rb
@@ -468,6 +468,8 @@ module ActiveRecord
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 13d8c68b33..2e24f8ebe1 100644
--- a/activerecord/test/cases/associations/has_many_associations_test.rb
+++ b/activerecord/test/cases/associations/has_many_associations_test.rb
@@ -1685,6 +1685,18 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
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
+
def test_building_has_many_association_with_restrict_dependency
option_before = ActiveRecord::Base.dependent_restrict_raises
ActiveRecord::Base.dependent_restrict_raises = true