如何解决软删除Rails Active Storage Blob和附件
有人可以帮助实现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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。