Skip to content

Commit 7f005e4

Browse files
committed
Move "avoid N+1 queries" explanation lower in the Active Storage documentation
The explanations about avoinding N+1 queries seems out of place early in the documentation. The actual association hasn't been explained yet and yet we explain performance optimizations. It's also confusing when reading the following paragraph where "this relationship" might seem to refer to the `with_attached_avatar` scope. Instead we can group it as a performance section together with the `strict_loading` section.
1 parent 18fcbd9 commit 7f005e4

File tree

1 file changed

+8
-8
lines changed
  • activestorage/lib/active_storage/attached

1 file changed

+8
-8
lines changed

activestorage/lib/active_storage/attached/model.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,6 @@ module Attached::Model
6161
# There is no column defined on the model side, Active Storage takes
6262
# care of the mapping between your records and the attachment.
6363
#
64-
# To avoid N+1 queries, you can include the attached blobs in your query like so:
65-
#
66-
# User.with_attached_avatar
67-
#
6864
# Under the covers, this relationship is implemented as a +has_one+ association to a
6965
# ActiveStorage::Attachment record and a +has_one-through+ association to a
7066
# ActiveStorage::Blob record. These associations are available as +avatar_attachment+
@@ -94,6 +90,10 @@ module Attached::Model
9490
# has_one_attached :avatar, service: ->(user) { user.in_europe_region? ? :s3_europe : :s3_usa }
9591
# end
9692
#
93+
# To avoid N+1 queries, you can include the attached blobs in your query like so:
94+
#
95+
# User.with_attached_avatar
96+
#
9797
# If you need to enable +strict_loading+ to prevent lazy loading of attachment,
9898
# pass the +:strict_loading+ option. You can do:
9999
#
@@ -163,10 +163,6 @@ def #{name}=(attachable)
163163
# There are no columns defined on the model side, Active Storage takes
164164
# care of the mapping between your records and the attachments.
165165
#
166-
# To avoid N+1 queries, you can include the attached blobs in your query like so:
167-
#
168-
# Gallery.where(user: Current.user).with_attached_photos
169-
#
170166
# Under the covers, this relationship is implemented as a +has_many+ association to a
171167
# ActiveStorage::Attachment record and a +has_many-through+ association to a
172168
# ActiveStorage::Blob record. These associations are available as +photos_attachments+
@@ -196,6 +192,10 @@ def #{name}=(attachable)
196192
# has_many_attached :photos, service: ->(gallery) { gallery.personal? ? :personal_s3 : :s3 }
197193
# end
198194
#
195+
# To avoid N+1 queries, you can include the attached blobs in your query like so:
196+
#
197+
# Gallery.where(user: Current.user).with_attached_photos
198+
#
199199
# If you need to enable +strict_loading+ to prevent lazy loading of attachments,
200200
# pass the +:strict_loading+ option. You can do:
201201
#

0 commit comments

Comments
 (0)