9

We have our app using uuids are primary keys, on a Postgresql Database. (Standard setup described here).

We integrated ActiveStorage following the process described here. A standard setup using rails active_storage:install and migrated using rails db:migrate.

We have a model & corresponding controller as follows:

# Model
class Message < ApplicationRecord
  has_one_attached :image

  def filename
    image&.attachment&.blob&.filename
  end
end

# Controller
class MessagesController < ApplicationController
  def create
    message = Message.create!(message_params)
    redirect_to message
  end

  private
    def message_params
      params.require(:message).permit(:title, :content, :image)
    end
end

We observed that the first few sets of image were correctly associated with the model instances, but then we used to get random images for model instance, or got no image at all. Every time, we restart the server, we get first few images right, but then it was unpredictable.

Unsure, of what's going wrong, we debugged in rails console:

params[:image]
=> #<ActionDispatch::Http::UploadedFile:0x007fcf2fa97b70 @tempfile=#<Tempfile:/var/folders/dt/05ncjr6s52ggc4bk6fs521qw0000gn/T/RackMultipart20180726-8503-vg36kz.pdf>, @original_filename="sample.pdf", @content_type="application/pdf", @headers="Content-Disposition: form-data; name=\"file\"; filename=\"sample.pdf\"\r\nContent-Type: application/pdf\r\n">

On saving the instance and retrieving file name we got a random file, we uploaded previously.

@message = Message.new(message_params)
@message.filename
=> #<ActiveStorage::Filename:0x007fcf32cfd9e8 @filename="sample.pdf">

@message.save

@message.filename
=> #<ActiveStorage::Filename:0x007f82f2ad4ef0 @filename="OtherSamplePdf.pdf"> 

Looking for an explanation for this weird behaviour, and a possible solution too.

3 Answers 3

39

After hours of going line by line in the activestorage source code, and running the same commands

@message = Message.new(message_params)
@message.save

again and again. We got same random results again and again. Then we went through the logs rails printed while attaching the image to message and observed the following:

S3 Storage (363.4ms) Uploaded file to key: KBKeHJARTjnsVjkgSbbii4Bz (checksum: S0GjR1EyvYYbMKh44wqlag==)

ActiveStorage::Blob Create (0.4ms)  INSERT INTO "active_storage_blobs" ("key", "filename", "content_type", "metadata", "byte_size", "checksum", "created_at") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id"  [["key", "KBKeHJARTjnsVjkgSbbii4Bz"], ["filename", "sample.pdf"], ["content_type", "application/pdf"], ["metadata", "{\"identified\":true}"], ["byte_size", 3028], ["checksum", "S0GjR1EyvYYbMKh44wqlag=="], ["created_at", "2018-07-26 04:54:33.029769"]]

ActiveStorage::Attachment Create (2.7ms)  INSERT INTO "active_storage_attachments" ("name", "record_type", "record_id", "blob_id", "created_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id"  [["name", "file"], ["record_type", "Message"], ["record_id", "534736"], ["blob_id", "0"], ["created_at", "2018-07-26 05:04:35.958831"]]

record_id was being set as 534736, instead of an uuid. Here's where we went wrong.

Active storage was expecting integer foreign key to our Message model, and we wanted it to use uuids instead. So we had to fix our migration, to use uuids instead of integer foreign keys.

Solution:

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

Hope this helps, someone facing similar issues. cheers!

6
  • 5
    Hello Anurag. I have the same issue. The problem is I have one model using integer as id, and one other model using uuid as id. Both models with attached files. So it's impossible for active storage to manage both types in record_id field in active_storage_attachments. That's a bad Active Storage limitation.I need to transform my 2 models with id as uuid now.
    – alex.bour
    Dec 13, 2018 at 8:01
  • We have one Document model with has_one attachment and use it to attach any document through foreign_key to different models, instead of having attachments in models. This way we limited the damage to just one model, and it worked out seamlessly. Although, am open to alternate implementations. Let me know.
    – anurag
    Dec 13, 2018 at 8:33
  • Thanks for your answer. In fact I have 2 models with has_one_attachment (images and photos) and I don't want to merge them, as they doesn't own the same fields. Well it's not a problem, finally I change the ID type for both as UUID and just change the record_it type to UUID in active_storage_attachments.
    – alex.bour
    Dec 13, 2018 at 8:55
  • 5
    If you've already applied the default migration, here are some migrations you can apply to fix your attachments table after the fact.
    – Kyle Fox
    Feb 4, 2019 at 1:04
  • ur a life saver
    – Welp
    Aug 7, 2020 at 15:10
6

I'm late to the party hitting this in 2020 but as anurag mentioned this is due to the active_storage_attachments DB table using a bigint for the record_id. I wasn't able to migrate all the models with ActiveStorage attachments to use UUIDs so I needed a way to support both UUIDs and bigints at the same time.

Warning: If you can avoid this (by migrating everything to UUID most likely) then I'd strongly recommend doing that, and I plan on doing that as soon as we have time.

Warnings aside, migrating the active_storage_attachments table to change the record_id column to be text does work. I had to adjust the few places in our app where we were joining against the active_storage_attachments table using record_id to cast the value to text in the join. For example I used the following code when joining to a model that had a UUID ID.

      .joins("
         LEFT OUTER JOIN active_storage_attachments
         ON active_storage_attachments.record_id = documents.id::text
       ")

Hopefully this helps someone else who is stuck in the halfway state where not all ActiveStorage using models are using UUIDs or bigint IDs.

0

I also had this issue. My models all use UUIDs. As I had no records in ActiveStorage needing to be retained, I dropped and recreated the :active_storage_attachments and :active_storage_blobs tables. Here is my migration, in case it's of use to anyone. Using Rails 6.0.4.

def change
  reversible do |dir|
    dir.up do
      drop_table :active_storage_attachments
      drop_table :active_storage_blobs
      drop_table :active_storage_variant_records

      create_table "active_storage_blobs", id: :uuid,
        default: -> { "gen_random_uuid()" }, force: :cascade 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,
        default: -> { "gen_random_uuid()" }, force: :cascade 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
        t.foreign_key :active_storage_blobs, column: :blob_id
      end
    end

      create_table :active_storage_variant_records, id: :uuid,
       default: -> { "gen_random_uuid()" }, force: :cascade do |t|
        t.belongs_to :blob, null: false, index: false, type: :uuid
        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, type: :uuid
   end

    dir.down do
      drop_table :active_storage_attachments
      drop_table :active_storage_blobs
      drop_table :active_storage_variant_records

      # original tables generated by rails
      create_table :active_storage_blobs 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 do |t|
        t.string     :name,     null: false
        t.references :record,   null: false, polymorphic: true, index: false
        t.references :blob,     null: false
        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
    end
  end
end
1
  • This is the right answer. But I later ran into issues because it hadn't migrated the variant records table. I've added an edit which provides the migration for the variant records table as well.
    – RJaus
    Jun 8 at 3:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.