Active Storage 附件
附件将记录与二进制大对象关联起来。通常是一对多记录-二进制大对象关系,但可以将许多不同的记录与同一个二进制大对象关联起来。附件表上的外键约束可防止在二进制大对象仍附加到任何记录时将其清除。
附件还可以访问 ActiveStorage::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
来源:显示 | 在 GitHub 上
# 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
。
来源:显示 | 在 GitHub 上
# File activestorage/app/models/active_storage/attachment.rb, line 31 belongs_to :blob, class_name: "ActiveStorage::Blob", autosave: true
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
。
来源:显示 | 在 GitHub 上
# File activestorage/app/models/active_storage/attachment.rb, line 101 def preview(transformations) transformations = transformations_by_name(transformations) blob.preview(transformations) end
purge() 链接
同步删除附件并 清除 blob。
来源:显示 | 在 GitHub 上
# 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。
来源:显示 | 在 GitHub 上
# 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 链接
返回关联记录。
来源:显示 | 在 GitHub 上
# File activestorage/app/models/active_storage/attachment.rb, line 25 belongs_to :record, polymorphic: true, touch: true
representation(transformations) 链接
返回 ActiveStorage::Preview
或 ActiveStorage::Variant
,用于具有所提供的 transformations
集的附件。示例
avatar.representation(resize_to_limit: [100, 100]).processed.url
或者,如果你使用预定义的变体
avatar.representation(:thumb).processed.url
有关更多信息,请参阅 ActiveStorage::Blob::Representable#representation
。
如果 transformations
是附件的未知预定义变体的 Symbol
,则引发 ArgumentError
。
来源:显示 | 在 GitHub 上
# 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::Variant
或 ActiveStorage::VariantWithRecord
实例,用于具有所提供的 transformations
集的附件。示例
avatar.variant(resize_to_limit: [100, 100]).processed.url
或者,如果你使用预定义的变体
avatar.variant(:thumb).processed.url
有关更多信息,请参阅 ActiveStorage::Blob::Representable#variant
。
如果 transformations
是附件的未知预定义变体的 Symbol
,则引发 ArgumentError
。
来源:显示 | 在 GitHub 上
# File activestorage/app/models/active_storage/attachment.rb, line 82 def variant(transformations) transformations = transformations_by_name(transformations) blob.variant(transformations) end