@@ -153,6 +153,30 @@ def test_with_attached_video_variant_no_n_plus_1
153
153
find_keys_for_representation "video.mp4"
154
154
end
155
155
156
+ def test_no_n_plus_1_with_all_variant_records_on_attached_video
157
+ user = User . create! ( name : "Justin" )
158
+
159
+ 10 . times do
160
+ blob = directly_upload_file_blob ( filename : "video.mp4" )
161
+ user . highlights_with_variants . attach ( blob )
162
+ end
163
+
164
+ # Force the processing
165
+ user . highlights_with_variants . each do |highlight |
166
+ highlight . representation ( :thumb ) . processed . key
167
+ end
168
+
169
+ user . reload
170
+
171
+ highlights = user . highlights_with_variants . with_all_variant_records . to_a
172
+
173
+ assert_queries_count ( 0 ) do
174
+ highlights . each do |highlight |
175
+ highlight . representation ( :thumb ) . key
176
+ end
177
+ end
178
+ end
179
+
156
180
test "eager loading has_many_attached records" do
157
181
user = User . create! ( name : "Josh" )
158
182
@@ -230,11 +254,12 @@ def test_with_attached_video_variant_no_n_plus_1
230
254
user . reload
231
255
232
256
assert_no_difference -> { ActiveStorage ::VariantRecord . count } do
233
- assert_queries_count ( 5 ) do
234
- # 5 queries:
257
+ assert_queries_count ( 6 ) do
258
+ # 6 queries:
235
259
# attachments (vlogs) x 1
236
260
# blobs for the vlogs x 1
237
261
# variant records for the blobs x 1
262
+ # preview_image_attachments for non-images
238
263
# attachments for the variant records x 1
239
264
# blobs for the attachments for the variant records x 1
240
265
user . vlogs . with_all_variant_records . each do |vlog |
@@ -275,11 +300,12 @@ def test_with_attached_video_variant_no_n_plus_1
275
300
# More queries here because we are creating a different variant.
276
301
# The second time we load this variant, we are back down to just 3 queries.
277
302
278
- assert_queries_match ( /SELECT/i , count : 9 ) do
303
+ assert_queries_match ( /SELECT/i , count : 10 ) do
279
304
# 9 queries:
280
305
# attachments (vlogs) initial load x 1
281
306
# blob x 1 (gets both records)
282
307
# variant record x 1 (gets both records)
308
+ # preview_image_attachments for non-images
283
309
# 2x get blob, attachment, variant records again, this happens when loading the new blob inside `VariantWithRecord#key`
284
310
user . vlogs . with_all_variant_records . each do |vlog |
285
311
rep = vlog . representation ( resize_to_limit : [ 200 , 200 ] )
@@ -291,7 +317,7 @@ def test_with_attached_video_variant_no_n_plus_1
291
317
292
318
user . reload
293
319
294
- assert_queries_count ( 5 ) do
320
+ assert_queries_count ( 6 ) do
295
321
user . vlogs . with_all_variant_records . each do |vlog |
296
322
rep = vlog . representation ( resize_to_limit : [ 200 , 200 ] )
297
323
rep . processed
0 commit comments