在我的项目中,我将纸夹和Paranoia gem一起使用(为了软删除一些模型)。在这个模型中,我同时使用两个gem:
class Material < ActiveRecord::Base
has_attached_file :material, preserve_files: true
acts_as_paranoid
validates_attachment :material, presence: true
endParanoia gem提供了一个硬删除对象的方法:really_destroy!方法。但是,当我调用此方法时,对象被删除,但文件被保留。我也删除了这个文件。示例:
@material_a.destroy # soft-delete the object and preserve the file
@material_b.really_destroy! # hard-delete the object and delete the file有没有办法动态设置回形针preserve_files选项?
发布于 2015-03-02 10:13:23
似乎您不能动态设置:preserve_files选项,但是有另一种方法可以执行您想要的操作。
回形针删除附件的方法是:首先设置要删除的路径队列,然后在保存对象时将其删除。如果存在同一对象的多个样式(例如,图像文件的不同大小),则对象可以具有多个要删除的路径。如果调用#destroy或#clear (不带参数),它将调用检查是否设置了:preserve_files的#queue_all_for_delete。但是如果你调用带有要删除的样式列表的#clear,它将调用不检查:preserve_files的#queue_some_for_delete。
所以,我们只需要为#clear提供一个所有样式的列表:
all_styles = @material_b.attachment.styles.keys.map { |key|
@material_b.attachment.styles[key].name
} << :original
@material_b.attachment.clear(*all_styles)
@material_b.save
@material_b.really_destroy!发布于 2016-03-02 04:04:59
这就是答案
has_attached_file :image, :styles => { :medium => "300x300#", :thumb => "100x100#", :small => "100x100#", :large => "500x500>"}, :convert_options => { :medium => "-quality 80 -interlace Plane", :thumb => "-quality 80 -interlace Plane" }, :default_url => "./public/images/avatar.png", :preserve_files => true, :processors => [:cropper]您只需添加,而无需添加任何额外的gem
:preserve_files => truehttps://stackoverflow.com/questions/27062924
复制相似问题