Class: ActiveStorage::Attachment

Inherits:
Record
  • Object
show all
Defined in:
app/models/active_storage/attachment.rb

Overview

Active Storage Attachment

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. A foreign-key constraint on the attachments table prevents blobs from being purged if they’re still attached to any records.

Attachments also have access to all methods from ActiveStorage::Blob.

If you wish to preload attachments or blobs, you can use these scopes:

# preloads attachments, their corresponding blobs, and variant records (if using `ActiveStorage.track_variants`)
User.all.with_attached_avatars

# preloads blobs and variant records (if using `ActiveStorage.track_variants`)
User.first.avatars.with_all_variant_records

Instance Method Summary collapse

Instance Method Details

#blobObject

:method:

Returns the associated ActiveStorage::Blob.



33
# File 'app/models/active_storage/attachment.rb', line 33

belongs_to :blob, class_name: "ActiveStorage::Blob", autosave: true

#preview(transformations) ⇒ Object

Returns an ActiveStorage::Preview instance for the attachment with the set of transformations provided. Example:

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

or if you are using pre-defined variants:

video.preview(:thumb).processed.url

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

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



100
101
102
103
# File 'app/models/active_storage/attachment.rb', line 100

def preview(transformations)
  transformations = transformations_by_name(transformations)
  blob.preview(transformations)
end

#purgeObject

Synchronously deletes the attachment and purges the blob.



50
51
52
53
54
55
56
# File 'app/models/active_storage/attachment.rb', line 50

def purge
  transaction do
    delete
    record.touch if record&.persisted?
  end
  blob&.purge
end

#purge_laterObject

Deletes the attachment and enqueues a background job to purge the blob.



59
60
61
62
63
64
65
# File 'app/models/active_storage/attachment.rb', line 59

def purge_later
  transaction do
    delete
    record.touch if record&.persisted?
  end
  blob&.purge_later
end

#recordObject

:method:

Returns the associated record.



27
# File 'app/models/active_storage/attachment.rb', line 27

belongs_to :record, polymorphic: true, touch: true

#representation(transformations) ⇒ Object

Returns an ActiveStorage::Preview or an ActiveStorage::Variant for the attachment with set of transformations provided. Example:

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

or if you are using pre-defined variants:

avatar.representation(:thumb).processed.url

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

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



119
120
121
122
# File 'app/models/active_storage/attachment.rb', line 119

def representation(transformations)
  transformations = transformations_by_name(transformations)
  blob.representation(transformations)
end

#variant(transformations) ⇒ Object

Returns an ActiveStorage::Variant or ActiveStorage::VariantWithRecord instance for the attachment with the set of transformations provided. Example:

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

or if you are using pre-defined variants:

avatar.variant(:thumb).processed.url

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

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



81
82
83
84
# File 'app/models/active_storage/attachment.rb', line 81

def variant(transformations)
  transformations = transformations_by_name(transformations)
  blob.variant(transformations)
end

#with_all_variant_recordsObject

:singleton-method:

Eager load all variant records on an attachment at once.

User.first.avatars.with_all_variant_records


47
# File 'app/models/active_storage/attachment.rb', line 47

scope :with_all_variant_records, -> { includes(blob: { variant_records: { image_attachment: :blob } }) }