简体   繁体   English

重新归档+ S3:删除附件并保留数据库记录

[英]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_filenamedocument_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.

相关问题 Carrierwave-从Amazon S3删除文件,但将记录保留在数据库中 - Carrierwave - Delete file from Amazon S3 but keep the record on db 带有重新归档的rails S3子文件夹 - rails S3 subfolders with refile 如何使用重新归档ActiveRecord :: RecordNotFound删除附件 - How to delete attachment using refile ActiveRecord::RecordNotFound 使用mongo db在Ruby模型上从HTTpary创建Refile附件 - Create Refile attachment from HTTpary on a Ruby model with mongo db 回形针-更新数据库中存在但S3中不存在的附件 - Paperclip - updating attachment that exists in DB but does not exist on S3 重新归档返回的Aws :: S3 :: Errors :: Forbidden:错误 - Refile returning Aws::S3::Errors::Forbidden: error 为生产和文件后端重新安装S3后端以用于开发模式 - Refile S3 backend for production and file backend for development mode 重新上传文件到S3仍返回本地主机上传 - Refile upload to S3 still returns localhost upload 是否可以在Refile初始化程序中为Rails 4项目中的不同功能配置单独的S3存储桶? - Can I configure separate S3 buckets in Refile initializer for different functionalities in a Rails 4 project? 如何通过Refile gem与rails集成来设置S3 ACL参数? - How can I set S3 ACL parameters with Refile gem integration with rails?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM