@@ -84,6 +84,44 @@ def test_setting_has_many_through_many_association_on_new_record_sets_through_re
84
84
assert_no_queries { assert_equal [ subscriber_1 , subscriber_2 ] . sort , book . subscribers . sort }
85
85
end
86
86
87
+ def test_setting_has_many_through_many_association_with_missing_targets_on_new_record_sets_empty_through_records
88
+ subscription_1 = Subscription . new
89
+ subscription_2 = Subscription . new
90
+ book = Book . new
91
+ book . subscriptions = [ subscription_1 , subscription_2 ]
92
+
93
+ assert_predicate book , :new_record?
94
+ book . subscriptions . each { |subscription | assert_predicate subscription , :new_record? }
95
+ assert_no_queries { assert_equal [ ] , book . subscribers }
96
+ end
97
+
98
+ def test_setting_has_many_through_many_association_with_partial_missing_targets_on_new_record_sets_partial_through_records
99
+ subscriber_1 = Subscriber . create! ( nick : "nick 1" )
100
+ subscription_1 = Subscription . new ( subscriber : subscriber_1 )
101
+ subscription_2 = Subscription . new
102
+ book = Book . new
103
+ book . subscriptions = [ subscription_1 , subscription_2 ]
104
+
105
+ assert_predicate subscriber_1 , :persisted?
106
+ assert_predicate book , :new_record?
107
+ book . subscriptions . each { |subscription | assert_predicate subscription , :new_record? }
108
+ assert_no_queries { assert_equal [ subscriber_1 ] , book . subscribers }
109
+ end
110
+
111
+ def test_setting_polymorphic_has_many_through_many_association_on_new_record_sets_through_records
112
+ human_1 , human_2 = Human . create! , Human . create!
113
+ interest_1 = Interest . new ( polymorphic_human : human_1 )
114
+ interest_2 = Interest . new ( polymorphic_human : human_2 )
115
+ zine = Zine . new
116
+ zine . interests = [ interest_1 , interest_2 ]
117
+
118
+ assert_predicate human_1 , :persisted?
119
+ assert_predicate human_2 , :persisted?
120
+ assert_predicate zine , :new_record?
121
+ zine . interests . each { |interest | assert_predicate interest , :new_record? }
122
+ assert_no_queries { assert_equal [ human_1 , human_2 ] . sort , zine . polymorphic_humans . sort }
123
+ end
124
+
87
125
def test_setting_nested_has_many_through_one_association_on_new_record_sets_nested_through_records
88
126
post_tagging_1 , post_tagging_2 = Tagging . create! , Tagging . create!
89
127
post = Post . create! ( title : "Tagged" , body : "Post" , taggings : [ post_tagging_1 , post_tagging_2 ] )
0 commit comments