class MigrateProductsToPaperclip < ActiveRecord::Migration def self.up # Rename the old "image" column to "old_file_name", since Product.image will now try to do Paperclip stuff rename_column :products, :image, :old_file_name # Rename asset directories to pluralize File.rename("public/system/product","public/system/products") File.rename("public/system/products/image","public/system/products/images") Product.all.each do |p| unless p.old_file_name.blank? # If an image was set by file_column, copy the relevant parameters to the new Paperclip columns p.update_attributes( :image_file_name => p.old_file_name, :image_content_type => 'image/jpeg', :image_updated_at => Time.now.utc, :image_file_size => File.stat("public/system/products/images/#{p.id}/#{p.old_file_name}").size) # Now rename the image file to add the original_ prefix File.rename("public/system/products/images/#{p.id}/#{p.old_file_name}","public/system/products/images/#{p.id}/original_#{p.old_file_name}") end end # And finally remove the old_file_name column, which we don't need any more remove_column :products, :old_file_name end def self.down # laziness, impatience, hubris raise IrreversibleMigration end end