インストールされているかどうかを確認
kiri@smtp:~/projects/rails/apas[1025]% gem list | grep activestorage activestorage (6.1.7.6) kiri@smtp:~/projects/rails/apas[1026]%
必要なテーブルの作成
kiri@smtp:~/projects/rails/apas[1028]% rails active_storage:install
Copied migration 20250224012151_create_active_storage_tables.active_storage.rb from active_storage
kiri@smtp:~/projects/rails/apas[1029]% rails db:migrate
Running via Spring preloader in process 50421
== 20250224012151 CreateActiveStorageTables: migrating ========================
-- create_table(:active_storage_blobs, {:id=>:primary_key})
-> 0.0443s
-- create_table(:active_storage_attachments, {:id=>:primary_key})
-> 0.0169s
-- create_table(:active_storage_variant_records, {:id=>:primary_key})
-> 0.0473s
== 20250224012151 CreateActiveStorageTables: migrated (0.1088s) ===============
作成されたテーブルの構成(db/migrate/20250224012151_create_active_storage_tables.active_storage.rb)
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 t.datetime :created_at, null: false 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 t.datetime :created_at, null: false 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
実際のイメージデータはこんなかんじ
#<ActiveStorage::Attachment id: 95, name: "images", record_type: "Hujimineko", record_id: 4, blob_id: 95, created_at: "2025-04-17 11:25:11.000000000 +0900">
とりあえずtestとlocalを使う(config/storage.yml)
test:
service: Disk
root: <%= Rails.root.join("tmp/storage") %>
local:
service: Disk
root: <%= Rails.root.join("storage") %>
使用するサービスを設定(config/environments/development.rb)
config.active_storage.service = :local