跳至内容 跳至搜索

Active Storage 附件

附件将记录与 blob 关联起来。通常这是一对多的记录与 blob 的关系,但也可以将多个不同的记录与同一个 blob 关联。附件表上的外键约束可防止 blob 在仍附加到任何记录时被删除。

附件还可以访问 ActiveStorage::Blob 中的所有方法。

如果您希望预加载附件或 blob,可以使用这些作用域

# 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)

返回附件的 ActiveStorage::Preview 实例,并带有提供的 transformations 集合。示例

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()

删除附件并排队一个后台作业来清除 blob。

# 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)

返回附件的 ActiveStorage::PreviewActiveStorage::Variant 实例,并带有提供的 transformations 集合。示例

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)

返回附件的 ActiveStorage::VariantActiveStorage::VariantWithRecord 实例,并带有提供的 transformations 集合。示例

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