I have a posts table and I want to add a video attachment (I'm using ActiveStorage).
I tried to create the following migration:
class AddVideoToPosts < ActiveRecord::Migration[7.0]
def change
add_column :posts, :video, :attachment
end
end
But when I run rails db:migrate
I get the following error:
ERROR: type "attachment" does not exist
Forgive me if this is a simple fix. I'm new to Rails. Thank you!
You don't need any migrations for some specific model attachments
Just add such line to model
class Post < ApplicationRecord
has_one_attached :video
end
or
class Post < ApplicationRecord
has_many_attached :videos
end
That's all
Of course it will work only if you set up Active Storage and have Active Storage tables
If you haven't such tables — add them with
bin/rails active_storage:install
bin/rails db:migrate
This sets up configuration, and creates the three tables Active Storage uses:
active_storage_blobs
stores data about uploaded files, such as filename and content type
active_storage_attachments
, a polymorphic join table that connects your models to blobs. If your model's class name changes, you will need to run a migration on this table to update the underlying record_type to your model's new class name
active_storage_variant_records
, if variant tracking is enabled, stores records for each variant that has been generated
Please see details in official guide