I made the mistake of using the default Paperclip url/path for multiple models, which is problem if an user upload a file with the same name and id, causing a conflict. Also, using the same directory for different models is kind of messy.
Here are the steps that i took to fix this problem:
1. Change the path/url in model
class User < ActiveRecord::Base has_attached_file :image end [/ruby] After: [ruby] class User < ActiveRecord::Base has_attached_file :image, :path => ":rails_root/public/system/users/images/:id/:style/:filename", :url => "/system/users/images/:id/:style/:filename" end
This will make them to be stored in public/system/users/images/. If you have other models like Post or Article, edit them too but use different paths.
2. Move your current data
If you already have some files, it’s time to move them as Paperclip won’t do it for you. To do it i created a rake task. Place it in lib/tasks/copy_paperclip_data.rake:
desc "Copy paperclip data" task :copy_paperclip_data => :environment do @users = User.find :all @users.each do |user| unless user.image_file_name.blank? filename = Rails.root.join('public', 'system', 'images', user.id.to_s, 'original', user.image_file_name) if File.exists? filename image = File.new filename user.image = image user.save image.close end end end end
You probably have to adapt this in case you are working on another model or field. In this case, i’m working on model Users and field image.
Note that i’m copying data. In case something goes wrong, you can revert it back. Remove it after you’re done.
Now simply run:
Hope this helps you.