aboutsummaryrefslogtreecommitdiffstats
path: root/app/models/active_storage/attachment.rb
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2017-07-24 12:05:15 -0500
committerDavid Heinemeier Hansson <david@loudthinking.com>2017-07-24 12:05:15 -0500
commit48d0427ff8dc6d338e2a05103a234872c32e718e (patch)
treef68a9e082eab482538e350855746549bd38469f4 /app/models/active_storage/attachment.rb
parentd0e90b4a9dc1accd4f1044fde0dd9a347cd0afcf (diff)
downloadrails-48d0427ff8dc6d338e2a05103a234872c32e718e.tar.gz
rails-48d0427ff8dc6d338e2a05103a234872c32e718e.tar.bz2
rails-48d0427ff8dc6d338e2a05103a234872c32e718e.zip
Basic documentation for all the models
Diffstat (limited to 'app/models/active_storage/attachment.rb')
-rw-r--r--app/models/active_storage/attachment.rb9
1 files changed, 9 insertions, 0 deletions
diff --git a/app/models/active_storage/attachment.rb b/app/models/active_storage/attachment.rb
index c3774306d8..2c8b7a9cf2 100644
--- a/app/models/active_storage/attachment.rb
+++ b/app/models/active_storage/attachment.rb
@@ -1,6 +1,10 @@
require "active_storage/blob"
require "active_support/core_ext/module/delegation"
+# Attachments associate records with blobs. Usually that's a one record-many blobs relationship,
+# but it is possible to associate many different records with the same blob. If you're doing that,
+# you'll want to declare with `has_one/many_attached :thingy, dependent: false`, so that destroying
+# any one record won't destroy the blob as well. (Then you'll need to do your own garbage collecting, though).
class ActiveStorage::Attachment < ActiveRecord::Base
self.table_name = "active_storage_attachments"
@@ -9,11 +13,16 @@ class ActiveStorage::Attachment < ActiveRecord::Base
delegate_missing_to :blob
+ # Purging an attachment will purge the blob (delete the file on the service, then destroy the record)
+ # and then destroy the attachment itself.
def purge
blob.purge
destroy
end
+ # Purging an attachment means purging the blob, which means talking to the service, which means
+ # talking over the internet. Whenever you're doing that, it's a good idea to put that work in a job,
+ # so it doesn't hold up other operations. That's what #purge_later provides.
def purge_later
ActiveStorage::PurgeJob.perform_later(self)
end