From c41247a3d273f415cf2c7aae7aff490234815ad9 Mon Sep 17 00:00:00 2001 From: Tobias Kraze Date: Tue, 27 Jun 2017 17:41:39 +0200 Subject: ActiveRecord: do not create "has many through" records that have been removed If a record was built on a HasManyThroughAssociation, then removed, and then the record was saved, the removed record would be created anyways. --- .../cases/associations/has_many_through_associations_test.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'activerecord/test/cases') diff --git a/activerecord/test/cases/associations/has_many_through_associations_test.rb b/activerecord/test/cases/associations/has_many_through_associations_test.rb index 9156f6d57a..1c2138a3d0 100644 --- a/activerecord/test/cases/associations/has_many_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb @@ -319,6 +319,17 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase assert_includes post.single_people, person end + def test_build_then_remove_then_save + post = posts(:thinking) + post.people.build(first_name: "Bob") + ted = post.people.build(first_name: "Ted") + post.people.delete(ted) + post.save! + post.reload + + assert_equal ["Bob"], post.people.collect(&:first_name) + end + def test_both_parent_ids_set_when_saving_new post = Post.new(title: "Hello", body: "world") person = Person.new(first_name: "Sean") -- cgit v1.2.3