软删除Rails Active Storage Blob和附件

问题描述

有人可以帮助实现Rails Active存储Blob和附件的软删除

我正在将act_as_paranoid gem用于其他模型,但是如何在Active storage 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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...