• Download Gist
Convert MySQL blob to file using Paperclip gem
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
namespace :images do
desc "converts blob to paperclip"
task :convert => :environment do
Image.find_each do |i|
puts i.id
tmp_file = File.dirname(__FILE__) + "/../../tmp/file.#{i.mime.gsub(/image\//,'')}"
File.open(tmp_file,'w') do |f|
f.write(i.data)
end
 
File.open(tmp_file,'r') do |f|
i.file = f
i.save!
end
end
end
end

Assuming you have an Image model with a blob field called "data":

  1. Add Paperclip fields to your model.
  2. Save the above as a rake task inside your Rails app's lib/tasks.
  3. In line 12, the "file" attribute should be changed to match the attribute used in "has_attached_file :file"
  4. Run rake images:convert

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.