9 Responses

  1. Icaro
    Icaro 17/05/2012 at 12:41 | | Reply

    I dont understand what your line 8 means. The filename doesnt exists because thats what we are attempting to do here. I dont get your code running, when i execute line 9, i got “No such file or directory”, because the old image doesnt exists in new directory but it does in the old directory.

    Any suggestions?

  2. Icaro
    Icaro 17/05/2012 at 15:10 | | Reply

    Perfect, now i understand everything.
    In my case, i need to delete the old original image. So i added FileUtils.rm image. This wont delete the entire folder, but the original image is using lots of hd space.


  3. Arif Usman
    Arif Usman 07/07/2012 at 3:44 | | Reply

    I have one issue. I want to make a copy of my previous image. By default the images are stored in the public folder. As soon as I make the clone of my existing image it start searching the image with the new id, but that folder doesn’t exist. For eg: if the id of my previous image is 5, then on cloning it start searching in the image in new folder with id = 6. But on cloning no such folder with id==6 creates. So I just want create a new folder with new id on cloning the image. How can I chieve that.

  4. Arif Usman
    Arif Usman 09/07/2012 at 10:19 | | Reply

    Ok is there any way to copy the same image with different id’s. I am going to paste the code which will make you understand what I want to tell you.

    I am creating an offer having an image attach with it using paper clip. As soon as I save that offer in the db I can view my attached image on show page. As my image is saved in following path and url:


    has_attached_file :image,
    :styles => {
    :thumb=> “100×100>”,
    :small => “150×150>”,
    :medium => “300×300>”,
    :large => “400×400>”
    },:path => “:rails_root/public/assets/products/:id/:style/:basename.:extension”,:url => “/assets/products/:id/:style/:basename.:extension”

    Now second time I want to clone that offer with same data and image. I am just copying all the things of particular offer and saving it with new id. Everything is saving fine, and the image is saving with new id but there is not any folder with that id in my public folder which used to create automatically using paper clip, hence it is not displaying any image. The code which I written in my controller is as follows:


    @deal = Offer.find(params[:id])
    @attr = { :nameofdeal => @deal.nameofdeal, :value => @deal.value, :offercategory => @deal.offercategory, :image_file_name => @deal.image_file_name,:type => ‘image/png’}
    @offer = Offer.new(@attr)

    So can you please help me how to achive that so that it will create a folder with the next id having the same image, or the path refer to the previous offer’s image whose folder is present in the public folder?

    Any help will be appreciated.

  5. Andrea Schiavini
    Andrea Schiavini 12/10/2012 at 6:45 | | Reply

    Hi Fernando, I used your code and slightly improved it for my project. You can see my version here: http://goo.gl/oyy2x . I credited you for your work, obviously – Thanks for sharing!

    Andrea Schiavini

  6. dalf
    dalf 14/09/2013 at 10:31 | | Reply

    Nice but the name of the file should be
    => copy_paperclip_data.rake
    instead of
    => copy_paperclip_data.rb

  7. ali abbas
    ali abbas 14/03/2017 at 8:53 | | Reply

    Here is another rake example for copying images using paper clip

    namespace :image_data do
    #bundle exec rake image_data:copy_paperclip_data
    desc “Copy paperclip data”
    task :copy_paperclip_data => :environment do
    @users = User.where([‘id = ?’,1])
    @users.each do |user|

    if user.images.present?
    user_img = user.images.first
    # filename = Rails.root.join(‘public’, ‘system’, ‘images’, user.id.to_s, ‘original’, user_img.image_file_name)

    filename = user_img.image.path

    if File.exists? filename
    puts filename
    image = File.new filename
    image1 = user.images.new
    image1.image = image
    puts image1.id


Leave a Reply