Skip to content

Instantly share code, notes, and snippets.

@zakelfassi
Created September 26, 2014 12:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zakelfassi/d37b9c8bf80ab6a08aa5 to your computer and use it in GitHub Desktop.
Save zakelfassi/d37b9c8bf80ab6a08aa5 to your computer and use it in GitHub Desktop.
Move Paperclip attachements
# 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