ActiveStorage的核心实际上是三个表(和模型),它们在某种程度上与图像表相对应:
class CreateActiveStorageTables < ActiveRecord::Migration[5.2]
def change
# Use Active Record's configured type for primary and foreign keys
primary_key_type, foreign_key_type = primary_and_foreign_key_types
create_table :active_storage_blobs, id: primary_key_type do |t|
t.string :key, null: false
t.string :filename, null: false
t.string :content_type
t.text :metadata
t.string :service_name, null: false
t.bigint :byte_size, null: false
t.string :checksum, null: false
if connection.supports_datetime_with_precision?
t.datetime :created_at, precision: 6, null: false
else
t.datetime :created_at, null: false
end
t.index [ :key ], unique: true
end
create_table :active_storage_attachments, id: primary_key_type do |t|
t.string :name, null: false
t.references :record, null: false, polymorphic: true, index: false, type: foreign_key_type
t.references :blob, null: false, type: foreign_key_type
if connection.supports_datetime_with_precision?
t.datetime :created_at, precision: 6, null: false
else
t.datetime :created_at, null: false
end
t.index [ :record_type, :record_id, :name, :blob_id ], name: "index_active_storage_attachments_uniqueness", unique: true
t.foreign_key :active_storage_blobs, column: :blob_id
end
create_table :active_storage_variant_records, id: primary_key_type do |t|
t.belongs_to :blob, null: false, index: false, type: foreign_key_type
t.string :variation_digest, null: false
t.index %i[ blob_id variation_digest ], name: "index_active_storage_variant_records_uniqueness", unique: true
t.foreign_key :active_storage_blobs, column: :blob_id
end
end
private
def primary_and_foreign_key_types
config = Rails.configuration.generators
setting = config.options[config.orm][:primary_key_type]
primary_key_type = setting || :primary_key
foreign_key_type = setting || :bigint
[primary_key_type, foreign_key_type]
end
end
从迁移中可以看出,它使用
active_storage_blobs
存储所存储文件的实际信息。一个blob也可以有多个变体。
active_storage_attachments
通过多态关联将blob与资源(连接附件的模型)连接起来。这让你可以添加
has_one_attached/has_many_attached
添加到应用程序中的任何模型,而无需添加任何其他数据库列或表。
因此,我正在考虑活动存储(未安装在此Rails应用程序上)是否可以与此现有设置配合使用。
让我们这样说吧——你不应该期望你可以直接将你的遗留数据插入到ActiveStorage中。这是一个非常固执己见的软件平台,主要围绕着能够以最少的配置插入任意数量的模型这一目标而设计。
ActiveStorage很可能可以与您现有的设置一起正常工作(将其替换为新记录),但将旧代码替换为AS很可能需要一些繁重的数据迁移,您还需要非常好地了解AS的工作方式。
我不明白的是活动存储和现有映像模型之间的关系。
那是因为根本没有。
ActiveSupport::Attachment
和
ActiveSupport::Blob
为Rails应用程序中具有附件的所有模型提供相同的角色。它的设计没有考虑到遗留支持。