簡體   English   中英

Rails 活動存儲 Blob 和附件的軟刪除

[英]Soft delete for Rails Active Storage Blobs and Attachments

有人可以幫助實現 Rails 活動存儲 Blob 和附件的軟刪除。

我正在將 act_as_paranoid gem 用於其他模型,但如何在活動存儲 blob 和附件中使用它。

如何覆蓋這個模型?

提前致謝。

所以我有一個似乎有效的可能解決方案。 我會很感激任何反饋,因為它感覺不是特別可靠。

在我的應用程序中, Employeesfiles ,我希望能夠存檔員工,然后在恢復員工記錄時取回他們的文件。

#/models/employee.rb
class Employee < ApplicationRecord
acts_as_paranoid  

def destroy
    @employee = Employee.find(params[:id])
    
    #create a temporary employee object to attach all files to.
    @temp_employee = Employee.new(name:"temp", position: "temp")

    #attach each of the original employee's files to the temp employee
    @employee.files.each do |f|
      @temp_employee.files.attach(f.blob)
    end

    #soft delete the employee
    @employee.destroy
    
    #re-attach all the files back 
    @temp_employee.files.each do |n|
      @employee.files.attach(n.blob)
    end
    
    @employee.save! 
   #employee is soft deleted, and the files are attached to the record
end
end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM