Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActiveStorage: record returns attached? as true but the blob is nil. How do I remove the "ghost" attachment? How did this happen?

This is in a production app. I suspect something happened during a file upload (direct uploads to S3) and the blob did not get created properly.

Now the app crashes in some cases and I am trying to remove the "ghost" attachment without deleting the parent record. Is this possible?

irb(main):020:0> s2.presentation_file.attached?
=> true

irb(main):021:0> s2.presentation_file.attachment
=> #<ActiveStorage::Attachment id: 362, name: "presentation_file", record_type: "Speaker", record_id: 219, blob_id: 369, created_at: "2018-05-06 19:44:06">

irb(main):022:0> s2.presentation_file.attachment.blob
=> nil

irb(main):023:0> s2.presentation_file.purge
NoMethodError: undefined method `purge' for nil:NilClass
from (irb):23
like image 838
aaandre Avatar asked Nov 08 '22 07:11

aaandre


1 Answers

I've no idea how it happened, but you can attach some image, just in order to purge it, thus healing the submission:

s2.presentation_file.attach(io: File.open("placeholder.jpg"), filename: 'placeholder.jpg')
s2.presentation_file.purge
like image 69
Adobe Avatar answered Nov 09 '22 22:11

Adobe