From c2120582e4d27d2a64131820a46392af60f1e8b6 Mon Sep 17 00:00:00 2001 From: Fabian Schwahn Date: Thu, 20 Apr 2017 11:10:06 +0200 Subject: [PATCH] Add regression test for setting inverse instances on normal & polymorphic relationships when building objects on new records --- .../associations/has_many_associations_test.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index e2f044c13964..40a0bea91b82 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -241,6 +241,13 @@ def test_create_from_association_with_nil_values_should_work assert_equal "defaulty", bulb.name end + def test_build_from_association_sets_inverse_instance + car = Car.new(name: "honda") + + bulb = car.bulbs.build + assert_equal car, bulb.car + end + def test_do_not_call_callbacks_for_delete_all car = Car.create(name: "honda") car.funky_bulbs.create! @@ -2146,6 +2153,13 @@ def test_build_with_polymorphic_has_many_does_not_allow_to_override_type_and_id assert_equal "Post", tagging.taggable_type end + def test_build_from_polymorphic_association_sets_inverse_instance + post = Post.new + tagging = post.taggings.build + + assert_equal post, tagging.taggable + end + def test_dont_call_save_callbacks_twice_on_has_many firm = companies(:first_firm) contract = firm.contracts.create!