我无法通过以下代码销毁附件图像:
@image = ActiveStorage::Blob.find(params[:id])
@image.purge
但是,如果这样做,我可以删除附件图像:
@image = ActiveStorage::Attachment.find(params[:id])
@image.purge
我的模型如下:
class Device < ApplicationRecord
has_many_attached :images
end
当我尝试通过实例化Blob
删除图像时,在清除时会看到以下回滚,但是打印@image.errors.to_json
只会显示一个空哈希。但是,我可以打印图像的id
和其他属性,因此我很肯定自己正在寻找并尝试删除正确的图像。
这是我的控制台输出,用于删除Blob
:
ActiveStorage::Blob Load (0.2ms) SELECT "active_storage_blobs".* FROM "active_storage_blobs" WHERE "active_storage_blobs"."id" = ? LIMIT ? [["id", 23], ["LIMIT", 1]]
↳ app/controllers/rooms_controller.rb:68
(0.1ms) begin transaction
↳ app/controllers/rooms_controller.rb:69
ActiveStorage::Attachment Exists (0.3ms) SELECT 1 AS one FROM "active_storage_attachments" WHERE "active_storage_attachments"."blob_id" = ? LIMIT ? [["blob_id", 23], ["LIMIT", 1]]
↳ app/controllers/rooms_controller.rb:69
(0.1ms) rollback transaction
↳ app/controllers/rooms_controller.rb:69