diff options
author | Kasper Timm Hansen <kaspth@gmail.com> | 2017-07-23 21:41:16 +0200 |
---|---|---|
committer | Kasper Timm Hansen <kaspth@gmail.com> | 2017-07-23 21:41:16 +0200 |
commit | a4f36f957e013f6da34e04f0d3f1d86d86491454 (patch) | |
tree | f6f3427278a90369f334b996c8e297c842b248af /lib/active_storage/attached/many.rb | |
parent | 212f925654f944067f18429ca02d902473214722 (diff) | |
download | rails-a4f36f957e013f6da34e04f0d3f1d86d86491454.tar.gz rails-a4f36f957e013f6da34e04f0d3f1d86d86491454.tar.bz2 rails-a4f36f957e013f6da34e04f0d3f1d86d86491454.zip |
Fix attaching with standard Rails associations.
Removes needless ivar caching (a Rails association handles that).
Inserts a reload and a nil assign, since the association proxy doesn't
seem to that it's been destroyed through `purge`.
Diffstat (limited to 'lib/active_storage/attached/many.rb')
-rw-r--r-- | lib/active_storage/attached/many.rb | 9 |
1 files changed, 4 insertions, 5 deletions
diff --git a/lib/active_storage/attached/many.rb b/lib/active_storage/attached/many.rb index 129f166776..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 ||= record.public_send("#{name}_attachments") + 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| + 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) 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 |