29
29
require "models/member"
30
30
require "models/membership"
31
31
require "models/club"
32
+ require "models/program"
33
+ require "models/program_offering"
34
+ require "models/enrollment"
32
35
require "models/organization"
33
36
require "models/user"
34
37
require "models/family"
41
44
require "models/zine"
42
45
require "models/interest"
43
46
require "models/human"
44
- require "models/account"
45
47
46
48
class HasManyThroughAssociationsTest < ActiveRecord ::TestCase
47
49
fixtures :posts , :readers , :people , :comments , :authors , :categories , :taggings , :tags ,
@@ -57,122 +59,6 @@ def setup
57
59
Reader . create person_id : 0 , post_id : 0
58
60
end
59
61
60
- def test_setting_has_many_through_one_association_on_new_record_sets_through_records
61
- account_1 , account_2 = Account . create! ( credit_limit : 100 ) , Account . create! ( credit_limit : 100 )
62
- firm = Firm . new ( accounts : [ account_1 , account_2 ] )
63
- client = Client . new
64
- client . firm = firm
65
-
66
- assert_predicate account_1 , :persisted?
67
- assert_predicate account_2 , :persisted?
68
- assert_predicate client , :new_record?
69
- assert_predicate client . firm , :new_record?
70
- assert_no_queries { assert_equal [ account_1 , account_2 ] . sort , client . accounts . sort }
71
- end
72
-
73
- def test_setting_has_many_through_many_association_on_new_record_sets_through_records
74
- subscriber_1 , subscriber_2 = Subscriber . create! ( nick : "nick 1" ) , Subscriber . create! ( nick : "nick 2" )
75
- subscription_1 = Subscription . new ( subscriber : subscriber_1 )
76
- subscription_2 = Subscription . new ( subscriber : subscriber_2 )
77
- book = Book . new
78
- book . subscriptions = [ subscription_1 , subscription_2 ]
79
-
80
- assert_predicate subscriber_1 , :persisted?
81
- assert_predicate subscriber_2 , :persisted?
82
- assert_predicate book , :new_record?
83
- book . subscriptions . each { |subscription | assert_predicate subscription , :new_record? }
84
- assert_no_queries { assert_equal [ subscriber_1 , subscriber_2 ] . sort , book . subscribers . sort }
85
- end
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
-
125
- def test_setting_nested_has_many_through_one_association_on_new_record_sets_nested_through_records
126
- post_tagging_1 , post_tagging_2 = Tagging . create! , Tagging . create!
127
- post = Post . create! ( title : "Tagged" , body : "Post" , taggings : [ post_tagging_1 , post_tagging_2 ] )
128
- author = Author . new ( name : "Josh" )
129
- author . posts = [ post ]
130
- categorization = Categorization . new
131
- categorization . author = author
132
-
133
- assert_predicate post_tagging_1 , :persisted?
134
- assert_predicate post_tagging_2 , :persisted?
135
- assert_predicate post , :persisted?
136
- assert_predicate categorization , :new_record?
137
- assert_predicate categorization . author , :new_record?
138
- assert_no_queries { assert_equal [ post_tagging_1 , post_tagging_2 ] . sort , categorization . post_taggings . sort }
139
- end
140
-
141
- def test_setting_nested_has_many_through_one_association_on_new_record_sets_targetless_nested_through_records
142
- post = Post . create! ( title : "Tagged" , body : "Post" )
143
- post_tagging_1 , post_tagging_2 = Tagging . create! ( taggable : post ) , Tagging . create! ( taggable : post )
144
- author = Author . new ( name : "Josh" )
145
- author . posts = [ post ]
146
- categorization = Categorization . new
147
- categorization . author = author
148
-
149
- assert_predicate post_tagging_1 , :persisted?
150
- assert_predicate post_tagging_2 , :persisted?
151
- assert_predicate post , :persisted?
152
- assert_predicate categorization , :new_record?
153
- assert_predicate categorization . author , :new_record?
154
- assert_queries_count ( 1 ) { assert_equal [ post_tagging_1 , post_tagging_2 ] . sort , categorization . post_taggings . sort }
155
- end
156
-
157
- def test_setting_nested_has_many_through_many_association_on_new_record_sets_nested_through_records
158
- account_1 = Account . create! ( firm_name : "account 1" , credit_limit : 100 )
159
- subscriber_1 = Subscriber . create! ( nick : "nick 1" , account : account_1 )
160
- account_2 = Account . create! ( firm_name : "account 2" , credit_limit : 100 )
161
- subscriber_2 = Subscriber . create! ( nick : "nick 2" , account : account_2 )
162
- subscription_1 = Subscription . new ( subscriber : subscriber_1 )
163
- subscription_2 = Subscription . new ( subscriber : subscriber_2 )
164
- book = Book . new
165
- book . subscriptions = [ subscription_1 , subscription_2 ]
166
-
167
- assert_predicate subscriber_1 , :persisted?
168
- assert_predicate subscriber_2 , :persisted?
169
- assert_predicate account_1 , :persisted?
170
- assert_predicate account_2 , :persisted?
171
- assert_predicate book , :new_record?
172
- book . subscriptions . each { |subscription | assert_predicate subscription , :new_record? }
173
- assert_no_queries { assert_equal [ account_1 , account_2 ] . sort , book . subscriber_accounts . sort }
174
- end
175
-
176
62
def test_has_many_through_create_record
177
63
assert books ( :awdr ) . subscribers . create! ( nick : "bob" )
178
64
end
@@ -1665,6 +1551,21 @@ def test_has_many_through_update_ids_with_conditions
1665
1551
assert_equal [ ] , author . nonspecial_categories_with_condition_ids
1666
1552
end
1667
1553
1554
+ def test_has_many_through_from_same_parent_to_same_child_creates_join_models
1555
+ club = Club . new ( name : "Awesome Rails Club" )
1556
+ member = club . simple_members . build ( name : "Jane Doe" )
1557
+
1558
+ program = Program . new ( name : "Learn Ruby on Rails" )
1559
+ program . members << member
1560
+
1561
+ club . programs << program
1562
+
1563
+ club . save!
1564
+
1565
+ assert_equal ( 1 , program . enrollments . size )
1566
+ assert_equal ( 1 , club . simple_memberships . size )
1567
+ end
1568
+
1668
1569
def test_single_has_many_through_association_with_unpersisted_parent_instance
1669
1570
post_with_single_has_many_through = Class . new ( Post ) do
1670
1571
def self . name ; "PostWithSingleHasManyThrough" ; end
0 commit comments