aboutsummaryrefslogtreecommitdiffstats
path: root/activestorage
diff options
context:
space:
mode:
authorEileen M. Uchitelle <eileencodes@users.noreply.github.com>2017-09-20 13:04:00 -0400
committerGitHub <noreply@github.com>2017-09-20 13:04:00 -0400
commite7f39af3fafbb83e93b3bb58a03aefbf10752f41 (patch)
tree9a2577def5913f8f229f7301053dd9717f345b42 /activestorage
parent057de4f7c99a91211e04aa55cab07c12c448a1ef (diff)
parent704bf9b9c64a2f92c6e72d74350bd8f00aaca5c5 (diff)
downloadrails-e7f39af3fafbb83e93b3bb58a03aefbf10752f41.tar.gz
rails-e7f39af3fafbb83e93b3bb58a03aefbf10752f41.tar.bz2
rails-e7f39af3fafbb83e93b3bb58a03aefbf10752f41.zip
Merge pull request #30629 from yhirano55/add_scope_to_has_one_attached_macro
Add `with_attached_*` scope to `has_one_attached` macro
Diffstat (limited to 'activestorage')
-rw-r--r--activestorage/lib/active_storage/attached/macros.rb6
-rw-r--r--activestorage/test/models/attachments_test.rb13
2 files changed, 19 insertions, 0 deletions
diff --git a/activestorage/lib/active_storage/attached/macros.rb b/activestorage/lib/active_storage/attached/macros.rb
index 35a081adc4..f0256718ac 100644
--- a/activestorage/lib/active_storage/attached/macros.rb
+++ b/activestorage/lib/active_storage/attached/macros.rb
@@ -12,6 +12,10 @@ module ActiveStorage
# There is no column defined on the model side, Active Storage takes
# care of the mapping between your records and the attachment.
#
+ # To avoid N+1 queries, you can include the attached blobs in your query like so:
+ #
+ # User.with_attached_avatar
+ #
# Under the covers, this relationship is implemented as a +has_one+ association to a
# ActiveStorage::Attachment record and a +has_one-through+ association to a
# ActiveStorage::Blob record. These associations are available as +avatar_attachment+
@@ -33,6 +37,8 @@ module ActiveStorage
has_one :"#{name}_attachment", -> { where(name: name) }, class_name: "ActiveStorage::Attachment", as: :record
has_one :"#{name}_blob", through: :"#{name}_attachment", class_name: "ActiveStorage::Blob", source: :blob
+ scope :"with_attached_#{name}", -> { includes("#{name}_attachment": :blob) }
+
if dependent == :purge_later
before_destroy { public_send(name).purge_later }
end
diff --git a/activestorage/test/models/attachments_test.rb b/activestorage/test/models/attachments_test.rb
index ac346c0087..379ae0a416 100644
--- a/activestorage/test/models/attachments_test.rb
+++ b/activestorage/test/models/attachments_test.rb
@@ -84,6 +84,19 @@ class ActiveStorage::AttachmentsTest < ActiveSupport::TestCase
end
end
+ test "find with attached blob" do
+ records = %w[alice bob].map do |name|
+ User.create!(name: name).tap do |user|
+ user.avatar.attach create_blob(filename: "#{name}.jpg")
+ end
+ end
+
+ users = User.where(id: records.map(&:id)).with_attached_avatar.all
+
+ assert_equal "alice.jpg", users.first.avatar.filename.to_s
+ assert_equal "bob.jpg", users.second.avatar.filename.to_s
+ end
+
test "attach existing blobs" do
@user.highlights.attach create_blob(filename: "funky.jpg"), create_blob(filename: "wonky.jpg")