class ActiveStorage::Attachment
User.first.avatars.with_all_variant_records
# preloads blobs and variant records (if using ‘ActiveStorage.track_variants`)
User.all.with_attached_avatars
# preloads attachments, their corresponding blobs, and variant records (if using `ActiveStorage.track_variants`)
If you wish to preload attachments or blobs, you can use these scopes:
Attachments also have access to all methods from ActiveStorage::Blob.
on the attachments table prevents blobs from being purged if they’re still attached to any records.
but it is possible to associate many different records with the same blob. A foreign-key constraint
Attachments associate records with blobs. Usually that’s a one record-many blobs relationship,
def analyze_blob_later
def analyze_blob_later blob.analyze_later unless blob.analyzed? end
def dependent
def dependent record.attachment_reflections[name]&.options&.fetch(:dependent, nil) end
def mirror_blob_later
def mirror_blob_later blob.mirror_later end
def purge
def purge transaction do delete record.touch if record&.persisted? end blob&.purge end
def purge_dependent_blob_later
def purge_dependent_blob_later blob&.purge_later if dependent == :purge_later end
def purge_later
def purge_later transaction do delete record.touch if record&.persisted? end blob&.purge_later end
def variant(transformations)
Raises an +ArgumentError+ if +transformations+ is a +Symbol+ which is an
See ActiveStorage::Blob::Representable#variant for more information.
instance for the attachment with the set of +transformations+ provided.
Returns an ActiveStorage::Variant or ActiveStorage::VariantWithRecord
def variant(transformations) case transformations when Symbol variant_name = transformations transformations = variants.fetch(variant_name) do record_model_name = record.to_model.model_name.name raise ArgumentError, "Cannot find variant :#{variant_name} for #{record_model_name}##{name}" end end blob.variant(transformations) end
def variants
def variants record.attachment_reflections[name]&.variants end