简体   繁体   中英

Refile + S3: delete attachment and keep db record

I'm using refile gem to upload files to S3 and having the following model

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.

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).

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.

I would go with defining 2 new columns in the respective model and copying the data to remain there upon removal.

This is way more consistent than keeping the malformed refile objects:

data_file.update_attributes!(
  file_name: data_file.document.filename,
  removed_at: DateTime.now,
  document: nil)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM