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,
= Active Storage Attachment

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 named_variants

def named_variants
  record.attachment_reflections[name]&.named_variants || {}
end

def preview(transformations)

unknown pre-defined variant of the attachment.
Raises an +ArgumentError+ if +transformations+ is a +Symbol+ which is an

See ActiveStorage::Blob::Representable#preview for more information.

video.preview(:thumb).processed.url

or if you are using pre-defined variants:

video.preview(resize_to_limit: [100, 100]).processed.url

Example:
of +transformations+ provided.
Returns an ActiveStorage::Preview instance for the attachment with the set
def preview(transformations)
  transformations = transformations_by_name(transformations)
  blob.preview(transformations)
end

def purge

Synchronously deletes the attachment and {purges the blob}[rdoc-ref:ActiveStorage::Blob#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

Deletes the attachment and {enqueues a background job}[rdoc-ref:ActiveStorage::Blob#purge_later] to purge the blob.
def purge_later
  transaction do
    delete
    record.touch if record&.persisted?
  end
  blob&.purge_later
end

def representation(transformations)

unknown pre-defined variant of the attachment.
Raises an +ArgumentError+ if +transformations+ is a +Symbol+ which is an

See ActiveStorage::Blob::Representable#representation for more information.

avatar.representation(:thumb).processed.url

or if you are using pre-defined variants:

avatar.representation(resize_to_limit: [100, 100]).processed.url

Example:
attachment with set of +transformations+ provided.
Returns an ActiveStorage::Preview or an ActiveStorage::Variant for the
def representation(transformations)
  transformations = transformations_by_name(transformations)
  blob.representation(transformations)
end

def transform_variants_later

def transform_variants_later
  preprocessed_variations = named_variants.filter_map { |_name, named_variant|
    if named_variant.preprocessed?(record)
      named_variant.transformations
    end
  }
  if blob.preview_image_needed_before_processing_variants? && preprocessed_variations.any?
    blob.create_preview_image_later(preprocessed_variations)
  else
    preprocessed_variations.each do |transformations|
      blob.preprocessed(transformations)
    end
  end
end

def transformations_by_name(transformations)

def transformations_by_name(transformations)
  case transformations
  when Symbol
    variant_name = transformations
    named_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.transformations
  else
    transformations
  end
end

def variant(transformations)

unknown pre-defined variant of the attachment.
Raises an +ArgumentError+ if +transformations+ is a +Symbol+ which is an

See ActiveStorage::Blob::Representable#variant for more information.

avatar.variant(:thumb).processed.url

or if you are using pre-defined variants:

avatar.variant(resize_to_limit: [100, 100]).processed.url

Example:
instance for the attachment with the set of +transformations+ provided.
Returns an ActiveStorage::Variant or ActiveStorage::VariantWithRecord
def variant(transformations)
  transformations = transformations_by_name(transformations)
  blob.variant(transformations)
end