LoginSignup
4
4

More than 5 years have passed since last update.

Paperclipのattachmentの削除方法

Last updated at Posted at 2014-12-24

destroyを使います。

class Picture < ActiveRecord::Base
  [:picture].each do |col|
    has_attached_file col,
    styles: {:medium => "300x300", :thumb => "100x100"},
    default_url: "common/:style/icon-file-nodata.png",
      url: "/data/images/:class/:parent_id/:id/:attachment/:style/:filename"
    validates_attachment_content_type col,
      :content_type => ["image/png", "image/jpg", "image/jpeg"]
  end
end
Picture.first
Picture.first.picture
#=> Paperclip::Attachment
Picture.first.picture.destroy
#=> 
[paperclip] deleting ~/pictures/1/pictures/original/5558.jpg
[paperclip] deleting ~/pictures/1/pictures/medium/5558.jpg
[paperclip] deleting ~/pictures/1/pictures/thumb/5558.jpg
[paperclip] deleting ~/pictures/1/pictures/large/5558.jpg
=> true

destroyはclear+saveだとリファレンスに書いてありますが、中身もそのまんまでした。

lib/paperclip/attachment.rb
# line 259
def destroy
  clear
  save
end
4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4