问题描述
有人可以帮助实现Rails Active存储Blob和附件的软删除。
我正在将act_as_paranoid gem用于其他模型,但是如何在Active storage Blob和附件中使用它。
如何覆盖此模型?
预先感谢。
解决方法
所以我有一个可行的解决方案。我会很感激任何反馈,因为它感觉不是特别可靠。
在我的应用程序中,Employees
有 files
,我希望能够存档员工,然后在恢复员工记录时取回他们的文件。
#/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