aboutsummaryrefslogtreecommitdiffstats
path: root/lib/active_storage/attached/macros.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/active_storage/attached/macros.rb')
-rw-r--r--lib/active_storage/attached/macros.rb6
1 files changed, 6 insertions, 0 deletions
diff --git a/lib/active_storage/attached/macros.rb b/lib/active_storage/attached/macros.rb
index 1e0f9a6b7e..0452089a5f 100644
--- a/lib/active_storage/attached/macros.rb
+++ b/lib/active_storage/attached/macros.rb
@@ -16,6 +16,9 @@ module ActiveStorage::Attached::Macros
instance_variable_set("@active_storage_attached_#{name}", ActiveStorage::Attached::One.new(name, self))
end
+ has_one :"#{name}_attachment", -> { where(name: name) }, class_name: "ActiveStorage::Attachment", as: :record
+ has_one :"#{name}_blob", through: :"#{name}_attachment"
+
if dependent == :purge_later
before_destroy { public_send(name).purge_later }
end
@@ -38,6 +41,9 @@ module ActiveStorage::Attached::Macros
instance_variable_set("@active_storage_attached_#{name}", ActiveStorage::Attached::Many.new(name, self))
end
+ has_many :"#{name}_attachments", -> { where(name: name) }, as: :record, class_name: "ActiveStorage::Attachment"
+ has_many :"#{name}_blobs", through: :"#{name}_attachments"
+
if dependent == :purge_later
before_destroy { public_send(name).purge_later }
end