跳至內容 跳至搜尋

Active Storage 附件

附件將記錄與 Blob 關聯起來。通常這是一種一對多的關係,但可以將許多不同的記錄與同一個 Blob 關聯。附件表格上的外鍵約束可防止 Blob 在仍附加到任何記錄時被清除。

附件也可以使用 ActiveStorage::Blob 的所有方法。

如果您希望預載附件或 Blob,您可以使用以下 scope

# 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
方法
B
P
R
V
W

類別公開方法

with_all_variant_records

一次性載入附件上的所有變體記錄。

User.first.avatars.with_all_variant_records
# File activestorage/app/models/active_storage/attachment.rb, line 45
scope :with_all_variant_records, -> { includes(blob: {
  variant_records: { image_attachment: :blob },
  preview_image_attachment: { blob: { variant_records: { image_attachment: :blob } } }

實例公開方法

blob

返回關聯的 ActiveStorage::Blob

# File activestorage/app/models/active_storage/attachment.rb, line 31
belongs_to :blob, class_name: "ActiveStorage::Blob", autosave: true, inverse_of: :attachments

preview(transformations)

使用提供的 transformations 集合,返回附件的 ActiveStorage::Preview 實例。範例

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

或者,如果您使用預先定義的變體

video.preview(:thumb).processed.url

更多資訊請參閱 ActiveStorage::Blob::Representable#preview

如果 transformations 是一個未知的預定義附件變體的 Symbol,則會引發 ArgumentError

# File activestorage/app/models/active_storage/attachment.rb, line 101
def preview(transformations)
  transformations = transformations_by_name(transformations)
  blob.preview(transformations)
end

purge()

同步刪除附件並清除 Blob

# File activestorage/app/models/active_storage/attachment.rb, line 51
def purge
  transaction do
    delete
    record.touch if record&.persisted?
  end
  blob&.purge
end

purge_later()

# File activestorage/app/models/active_storage/attachment.rb, line 60
def purge_later
  transaction do
    delete
    record.touch if record&.persisted?
  end
  blob&.purge_later
end

record

返回關聯的記錄。

# File activestorage/app/models/active_storage/attachment.rb, line 25
belongs_to :record, polymorphic: true, touch: ActiveStorage.touch_attachment_records

representation(transformations)

使用提供的 transformations 集合,返回附件的 ActiveStorage::PreviewActiveStorage::Variant 實例。範例

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

或者,如果您使用預先定義的變體

avatar.representation(:thumb).processed.url

更多資訊請參閱 ActiveStorage::Blob::Representable#representation

如果 transformations 是一個未知的預定義附件變體的 Symbol,則會引發 ArgumentError

# File activestorage/app/models/active_storage/attachment.rb, line 120
def representation(transformations)
  transformations = transformations_by_name(transformations)
  blob.representation(transformations)
end

variant(transformations)

使用提供的 transformations 集合,返回附件的 ActiveStorage::VariantActiveStorage::VariantWithRecord 實例。範例

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

或者,如果您使用預先定義的變體

avatar.variant(:thumb).processed.url

更多資訊請參閱 ActiveStorage::Blob::Representable#variant

如果 transformations 是一個未知的預定義附件變體的 Symbol,則會引發 ArgumentError

# File activestorage/app/models/active_storage/attachment.rb, line 82
def variant(transformations)
  transformations = transformations_by_name(transformations)
  blob.variant(transformations)
end