Shrine sometimes doesn't upload file even with 200 OK

I have a shrine upload endpoint that is used to upload files to an object before the object is created. Most of the time it works. I’m not sure what cause it to fail. Looking at the network panel in Chrome devtools, the upload returns 200 OK. Then when I try to save the associated object, I get a 500 Internal Server Error cause by a Shrine::FileNotFound. Previously removing the file from the object and re-adding it worked, but this time, it didn’t work until I tried it on a different computer. Other files worked fine on the first computer.

I did an ls | grep in the cache and storage directory, and the file wasn’t in either of those places. My shrine initializer is below. I’m wondering if it has anything to do with the upload_options.

require "shrine"
require "shrine/storage/file_system"

Shrine.storages = {
  cache: Shrine::Storage::FileSystem.new("/usr/docs/ap/cache", prefix: ""), # temporary
  store: Shrine::Storage::FileSystem.new("/usr/docs/ap", prefix: ""),       # permanent
}

Shrine.plugin :activerecord # or :sequel
Shrine.plugin :cached_attachment_data # for retaining the cached file across form redisplays
Shrine.plugin :restore_cached_data # re-extract metadata when attaching a cached file
Shrine.plugin :instrumentation, logger: Rails.logger
Shrine.plugin :determine_mime_type
Shrine.plugin :upload_options, cache: { move: true }, store: { move: true }

Here is my uploader:

class ApFileUploader < Shrine
  # plugins and uploading logic
  plugin :upload_endpoint, max_size: 20*1024*1024 # 20 MB

  def generate_location(io, record: nil, derivative: nil, **)
    return super unless record

    table = record.class.table_name
    table, id =
      if record.class == ApFile && record.has_ap_file_type == 'ApInvoice'
        [record.has_ap_file.class.table_name, record.has_ap_file.invoice_date]
      else
        record.class.table_name
        record.id
      end
    prefix = derivative ? derivative.to_s + ' - ' : ''
    location = "#{table}/#{id}/#{prefix}#{super}"
  end
end

The file is only 151 KB, so I don’t think it would be an issue with the file size.

Anyone know what could be causing the issue? Any help is appreciated. Thank you.

I think that if I try to save my object and it returns with 500 Internal Server Error, then I need to re-upload the file. Any idea why the file wouldn’t exist after an internal server error?

I have this in the create method
invoice = ApInvoice.new ap_invoice_params

Here is the relevant part of ap_invoice_params

params.require(:ap_invoice).permit(
  {files_attributes: :file_data},
  {files_attributes: :file},
)