diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2017-07-23 17:00:01 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-07-23 17:00:01 -0500 |
commit | 54b17a1c8323c6ec3a7d6e521179ef36d396d285 (patch) | |
tree | 3ebc65daf6d6bfd904a455afc8008478a2b01d55 /lib/active_storage/attached/many.rb | |
parent | c977eef67b5c64932064bc98d2bb293315afc65a (diff) | |
parent | 2bbfaf0c9f6ad23cb2c64a917848ca180917ebe2 (diff) | |
download | rails-54b17a1c8323c6ec3a7d6e521179ef36d396d285.tar.gz rails-54b17a1c8323c6ec3a7d6e521179ef36d396d285.tar.bz2 rails-54b17a1c8323c6ec3a7d6e521179ef36d396d285.zip |
Merge pull request #32 from rails/explore-polymorphism
Use regular polymorphic associations rather than record_gid
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 |