Read more

Copy a Paperclip attachment to another record

Tobias Kraze
August 24, 2010Software engineer at makandra GmbH

Just assign the existing attachment to another record:

new_photo = Photo.new
new_photo.image = old_photo.image
Illustration UI/UX Design

UI/UX Design by makandra brand

We make sure that your target audience has the best possible experience with your digital product. You get:

  • Design tailored to your audience
  • Proven processes customized to your needs
  • An expert team of experienced designers
Read more Show archive.org snapshot

Paperclip will duplicate the file when saving.

To use this in forms, pimp your attachment container like this:

class Photo < ActiveRecord::Base
  has_attached_file :image
  
  attr_accessor :copy_of

  def image_url
    if copy_of
      copy_of.url
    else
      image.url
    end
  end
end

And in the controller do:

new_photo = Photo.new(:copy_of => old_photo)
Posted by Tobias Kraze to makandra dev (2010-08-24 19:30)