diff options
Diffstat (limited to 'activestorage/lib/active_storage/attached')
-rw-r--r-- | activestorage/lib/active_storage/attached/macros.rb | 12 |
1 files changed, 9 insertions, 3 deletions
diff --git a/activestorage/lib/active_storage/attached/macros.rb b/activestorage/lib/active_storage/attached/macros.rb index fbd64b5edc..5779348148 100644 --- a/activestorage/lib/active_storage/attached/macros.rb +++ b/activestorage/lib/active_storage/attached/macros.rb @@ -23,8 +23,11 @@ module ActiveStorage # (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 @@ -61,8 +64,11 @@ module ActiveStorage # (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" @@ -75,4 +81,4 @@ module ActiveStorage end end end -end +end
\ No newline at end of file |