diff options
Diffstat (limited to 'lib/active_storage/attached/many.rb')
-rw-r--r-- | lib/active_storage/attached/many.rb | 11 |
1 files changed, 5 insertions, 6 deletions
diff --git a/lib/active_storage/attached/many.rb b/lib/active_storage/attached/many.rb index 99d980196a..704369ba89 100644 --- a/lib/active_storage/attached/many.rb +++ b/lib/active_storage/attached/many.rb @@ -7,15 +7,15 @@ class ActiveStorage::Attached::Many < ActiveStorage::Attached # You don't have to call this method to access the attachments' methods as # they are all available at the model level. def attachments - @attachments ||= ActiveStorage::Attachment.where(record_gid: record.to_gid.to_s, name: name) + record.public_send("#{name}_attachments") end # Associates one or several attachments with the current record, saving # them to the database. def attach(*attachables) - @attachments = attachments | Array(attachables).flatten.collect do |attachable| - ActiveStorage::Attachment.create!(record_gid: record.to_gid.to_s, name: name, blob: create_blob_from(attachable)) - end + record.public_send("#{name}_attachments=", attachments | Array(attachables).flat_map do |attachable| + ActiveStorage::Attachment.create!(record: record, name: name, blob: create_blob_from(attachable)) + end) end # Checks the presence of attachments. @@ -34,7 +34,7 @@ class ActiveStorage::Attached::Many < ActiveStorage::Attached def purge if attached? attachments.each(&:purge) - @attachments = nil + attachments.reload end end @@ -42,7 +42,6 @@ class ActiveStorage::Attached::Many < ActiveStorage::Attached def purge_later if attached? attachments.each(&:purge_later) - @attachments = nil end end end |