[英]Refile + S3: delete attachment and keep db record
I'm using refile
gem to upload files to S3
and having the following model 我正在使用
refile
gem将文件上传到S3
并具有以下模型
class DataFile < ActiveRecord::Base
attachment :document, destroy: true
end
The respective table in database has columns document_filename
and document_id
to hold file metadata. 数据库中的相应表具有
document_filename
和document_id
列以保存文件元数据。
I need to remove file from S3
and keep the respective row in DB (I need that to be able to display the name and delete date of that file). 我需要从
S3
删除文件,并将相应的行保留在数据库中(我需要该行才能显示该文件的名称和删除日期)。
I'm trying to do 我在努力
data_file.document = nil
data_file.save()
But that removes filename. 但这会删除文件名。 Is there a solution to remove file from S3 and keep
document_filename
value. 是否有解决方案,可以从S3中删除文件并保持
document_filename
值。
I would go with defining 2 new columns in the respective model and copying the data to remain there upon removal. 我将在相应的模型中定义2个新列,并复制数据以在删除后保留在那里。
This is way more consistent than keeping the malformed refile
objects: 这比保留格式错误的重新
refile
对象更加一致:
data_file.update_attributes!(
file_name: data_file.document.filename,
removed_at: DateTime.now,
document: nil)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.