copying an image from one mount (+model) to another

1,283 views
Skip to first unread message

Joey

unread,
Dec 7, 2011, 6:10:03 PM12/7/11
to carrierwave
Hi all,

I have a need to occasionally populate the image(s) stored on one
model's uploader from another. E.g.,

class Artwork
mount_uploader :image, ArtworkUploader
end

class Artist
mount_uploader :image, ArtistUploader

# copy an artwork image into the artist and perform necessary
processing
def replace_image_from(artwork)
artwork.image.cache_stored_file! unless artwork.image.cached?
self.image_cache = artwork.image_cache
self.store_image!
end
end

This almost worked, but our uploaders increment a version number that
goes into the store_path, and it turns out that calling
artwork.image.cache_stored_file! increments the version number on the
artwork, which actually hasn't changed. Is there a simpler way to read
the original file from one mount point into another?

Thanks,
Joey

Joey

unread,
Dec 10, 2011, 9:03:27 PM12/10/11
to carrierwave
For those interested, this somewhat simpler approach has the desired
effect:

class Artist
  mount_uploader :image, ArtistUploader
  def replace_image_from(artwork)    self.image = artwork.image.file 
  self.save!  endend

equivalent8

unread,
Dec 12, 2014, 4:26:14 AM12/12/14
to carri...@googlegroups.com
Don't know if this is still relevant but you can use my gem ( or just copy code from it, it's just one file)  https://github.com/equivalent/copy_carrierwave_file 

with it you can do something like: 

original_resource = Artist.last
new_resource      = Artwork.new

CopyCarrierwaveFile::CopyFileService.new(original_resource, new_resource, :image).set_file
  # :avatar represents mount point (field)

new_resource.save
Reply all
Reply to author
Forward
0 new messages