如何安全地删除回形针附件上的链接?

How safetly remove links on paperclip attachmetns?

我有带 mysql 数据库的 Paperclip 附件管理器。

class Photo < ActiveRecord::Base

  has_attached_file :file, :styles => {:large => "800x600>"}, :default_url => "/images/:style/missing.png"

end

图像存储在 Amazon S3 中。我需要从数据库中删除此图像上的链接,但图像必须保留在 S3 上。我该怎么做?

1) Photo.delete_all?

2) 从 ActiveAdmin 中删除?

3) 从 mysql 控制台删除?

4) 另一种方法......?

您可以尝试将 file_url 全部设为空白,无需删除所有照片

Photo.update_all(file: nil)

keep_old_files:

Keep the existing attachment files (original + resized) from being automatically deleted when an attachment is cleared or updated. Defaults to false.

也许你可以使用:

#app/models/photo.rb
class Photo < ActiveRecord::Base

    has_attached_file :file,
      styles: {:large => "800x600>"},
      default_url: "/images/:style/missing.png",
      keep_old_files: true

end

...甚至更好:

preserve_files

Keep the existing attachment files in all cases, even if the parent record is destroyed.

#app/models/photo.rb
class Photo < ActiveRecord::Base

    has_attached_file :file,
      styles: {:large => "800x600>"},
      default_url: "/images/:style/missing.png",
      preserve_files: true

end