Skip to content

How to: migrate from paperclip to carrierwave (example thor task)

sekrett edited this page Apr 6, 2012 · 1 revision
class MyApp < Thor
  desc :migrate_from_paperclip_to_carrierwave, 'Migrate attachments from paperclip to carrierwave'
  def migrate_from_paperclip_to_carrierwave
    require File.expand_path 'config/environment'

    FileUtils.mkdir_p Rails.root.join('public', 'uploads')

    Product.where('image_file_name IS NOT NULL').each do |product|
      product.image = File.open(Rails.root.join('public', 'system', 'images', product.id.to_s, 'original', product.image_file_name))
      product.save!
    end
  end
end
Clone this wiki locally