如何在删除列时删除对应于列的图像

How to remove images corresponding to a column when removing the column

我在我的项目中使用 rails 4.2.1、ruby 2.3.0p0 和 carrierwave(0.11.0)。目前我有一个带有图像列的模型产品。现在,我想为同一产品保存多张图片。所以我创建了一个新模型 ProductImage 并使用了以下代码

Product.all.each do |prdct|
  if prdct.image.present?
    @photo = prdct.product_images.create!(image: prdct.image)
    prdct.remove_image!
  end
end
remove_column :products, :image

但是保存在 product/image 文件夹中的图像没有被删除。在 ProductImage 模型

下移动图像后如何删除图像

prdct.remove_image! 之后,您必须保存 (prdct.save) 对象以便将其删除。

如果您也想删除该文件夹,则必须执行如下操作:

def remove_image_directory
 FileUtils.remove_dir("#{Rails.root}/path/to/folder/", force: true)
end

删除图片后调用此方法

来自文档: https://github.com/carrierwaveuploader/carrierwave#removing-uploaded-files