Select Page

Rails 6 ActiveStorage ‘out of the box’ assumes the use of standard id’s. If you are using a uuid on the related models, you will not immediately run into errors.

However, if you try the following:

[code language=”ruby”]
User.joins(:photo_attachment)
[/code]

You will hit an error like this:

[code language=”bash”] activestorage ERROR: operator does not exist: bigint = uuid [/code]

To fix this, tweak the generated ActiveStorage migration to use uuid’s itself and also to reference foreign keys as uuid’s.

Add the {id: :uuid} option to all instances of create_table. Add the {type: :uuid} option to all instances of references.

[code language=”ruby” title=”2019xxxxxxxxx_create_active_storage_tables.active_storage.rb”]
class CreateActiveStorageTables < ActiveRecord::Migration[5.2]
def change
create_table :active_storage_blobs, id: :uuid do |t|
t.string :key, null: false
t.string :filename, null: false
t.string :content_type
t.text :metadata
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: :uuid do |t|
t.string :name, null: false
t.references :record, null: false, polymorphic: true, index: false, type: :uuid
t.references :blob, null: false, type: :uuid

t.datetime :created_at, null: false

t.index [ :record_type, :record_id, :name, :blob_id ], name: ‘index_active_storage_attachments_uniqueness’, unique: true
end
end
end
[/code]