ActiveStorage:附加记录回报?虽然如此,但blob是零。如何删除“幽灵”附件?这是怎么发生的?

时间:2018-05-07 22:23:23

标签: ruby-on-rails ruby-on-rails-5 attachment rails-activestorage ruby-on-rails-5.2

这是在制作应用中。我怀疑在文件上传过程中发生了什么(直接上传到S3)并且没有正确创建blob。

现在应用程序在某些情况下崩溃了,我试图在不删除父记录的情况下删除“ghost”附件。这可能吗?

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

1 个答案:

答案 0 :(得分:0)

我不知道它是如何发生的,但是您可以附加一些图像,只是为了清除它,从而修复提交的内容:

s2.presentation_file.attach(io: File.open("placeholder.jpg"), filename: 'placeholder.jpg')
s2.presentation_file.purge