Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix CollectionAssociation#replace to return new target (closes #6231)

  • Loading branch information...
commit e2a070cf47fb9a845ecf53ff2f437390598fe0b7 1 parent 1f0e2de
@drogus drogus authored
View
2  activerecord/lib/active_record/associations/collection_association.rb
@@ -468,6 +468,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)
View
12 activerecord/test/cases/associations/has_many_associations_test.rb
@@ -1685,6 +1685,18 @@ def test_replace
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
Please sign in to comment.
Something went wrong with that request. Please try again.