Created
September 26, 2014 12:54
-
-
Save zakelfassi/d37b9c8bf80ab6a08aa5 to your computer and use it in GitHub Desktop.
Move Paperclip attachements
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Assuming you had a model like this | |
# | |
# class Post | |
# has_attached_file :image, :path => ":rails_root/public/system/:attachment/:id/:style/:filename" | |
# end | |
namespace :paperclip do | |
desc "Recreate attachments and save them to new destination" | |
task :move_attachments => :environment do | |
Post.find_each do |post| | |
unless post.image_file_name.blank? | |
filename = Rails.root.join('public', 'system', 'images', post.id.to_s, 'original', post.image_file_name) | |
if File.exists? filename | |
puts "Re-saving image attachment #{post.id} - #{filename}" | |
image = File.new filename | |
post.image = image | |
post.save | |
# if there are multiple styles, you want to recreate them : | |
post.image.reprocess! | |
image.close | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment