aboutsummaryrefslogtreecommitdiffstats
path: root/activestorage/lib/active_storage/attached
diff options
context:
space:
mode:
Diffstat (limited to 'activestorage/lib/active_storage/attached')
-rw-r--r--activestorage/lib/active_storage/attached/macros.rb10
1 files changed, 8 insertions, 2 deletions
diff --git a/activestorage/lib/active_storage/attached/macros.rb b/activestorage/lib/active_storage/attached/macros.rb
index 89297e5bdf..f09f3e1f6d 100644
--- a/activestorage/lib/active_storage/attached/macros.rb
+++ b/activestorage/lib/active_storage/attached/macros.rb
@@ -22,8 +22,11 @@ module ActiveStorage::Attached::Macros
# (i.e. destroyed) whenever the record is destroyed.
def has_one_attached(name, dependent: :purge_later)
define_method(name) do
- instance_variable_get("@active_storage_attached_#{name}") ||
+ if instance_variable_defined?("@active_storage_attached_#{name}")
+ instance_variable_get("@active_storage_attached_#{name}")
+ else
instance_variable_set("@active_storage_attached_#{name}", ActiveStorage::Attached::One.new(name, self))
+ end
end
has_one :"#{name}_attachment", -> { where(name: name) }, class_name: "ActiveStorage::Attachment", as: :record
@@ -60,8 +63,11 @@ module ActiveStorage::Attached::Macros
# (i.e. destroyed) whenever the record is destroyed.
def has_many_attached(name, dependent: :purge_later)
define_method(name) do
- instance_variable_get("@active_storage_attached_#{name}") ||
+ if instance_variable_defined?("@active_storage_attached_#{name}")
+ instance_variable_get("@active_storage_attached_#{name}")
+ else
instance_variable_set("@active_storage_attached_#{name}", ActiveStorage::Attached::Many.new(name, self))
+ end
end
has_many :"#{name}_attachments", -> { where(name: name) }, as: :record, class_name: "ActiveStorage::Attachment"