21

I have two models like this:-

Model 1 - card - contains a representation of data of interest for front page
attachment name = cardimage
Model 2 - user - contains the user
attachment name = avatar

When I create! a new card, I want the avatar from the user model to be copied to the card model as a new cardimage.

Is there a simple one liner for this?

Ruby/Rails/Paperclip

Gary
  • 1,917
  • 3
  • 19
  • 19
  • possible duplicate http://stackoverflow.com/questions/2739839/how-to-copy-a-file-using-paperclip – cih Jan 08 '13 at 21:16
  • 1
    Do you need to store the attachment twice? If your User model is associated with the Card model you could avoid duplication. – cih Jan 08 '13 at 21:19
  • That example is copying from one users profile to another. What is needed is to copy the image to a different model. – Gary Jan 08 '13 at 21:20
  • The card model is not associated. It is used independently to store random information and images from any model, real world data, tweets etc for a front page summary – Gary Jan 08 '13 at 21:22
  • So how will you know which user you ant the avatar from, current_user? – cih Jan 08 '13 at 21:26
  • that will already be available in an instance variable.. – Gary Jan 08 '13 at 21:52

4 Answers4

37

This should do the trick, you could use an after_create callback if the models are associated, if not I would recommend doing it in the controller action that creates the card.

instance_of_model_one.cardimage = instance_of_model_two.avatar
instance_of_model_one.save
cih
  • 1,960
  • 1
  • 16
  • 27
  • This works correctly. I tried with the file name initially, but just the method is correct, paperclip handles the rest quite nicely, with correct id's etc. Thank you. – Gary Jan 08 '13 at 22:50
  • I think you need `instance_of_model_two.avatar.dup` – Cosmin Atanasiu Jan 04 '14 at 00:57
  • 1
    Nope, pretty sure you don't need to `dup` in this case. – cih Jan 04 '14 at 15:58
  • Attachment styles are copied from the original url, then reprocessed. When duplicating manually reprocessed attachment, new_model.image.url(:thumbnail) is copied from original_model.image.url(:original) and reprocessed. Unless one saves the reprocess parameters (e.g. cropping) - no reprocessing is performed and all styles look the same as the original. I found an issue about it and offer a solution for manual copying: http://stackoverflow.com/questions/17603045/copy-all-styles-of-a-paperclip-attachment-to-new-object-s3 – Tal Yaniv Jun 19 '14 at 04:39
  • 2
    https://github.com/thoughtbot/paperclip/issues/9#issuecomment-609304 works for S3 – shweta Nov 28 '16 at 10:02
3
old_avatar = model1.avatar;
model2.avatar.create(attachment: old_avatar.attachment);
model2.save;

It worked for me.

scc
  • 31
  • 1
1

Suppose you have 2 models:

  • User
  • Player

You have to copy profile_image from User with id = 1 to Player with id = 10. You can perform the following:

user = User.find(1)
player = Player.find(10)

player.profile_image = user.profile_image
player.save!

Sometimes this might save the file but with a file size of 0 Bytes. In such cases try the following:

user = User.find(1)
player = Player.find(10)

player.profile_image = user.profile_image.url
player.save!

This shall do the work!

Pushp Raj Saurabh
  • 1,174
  • 12
  • 16
0

Ideally, the approach mentioned by @cih should work:

instance_of_model_one.cardimage = instance_of_model_two.avatar
instance_of_model_one.save

But in my case, it was creating a blank file in S3. To resolve the issue, I tried the below approach and it worked:

instance_of_model_one.cardimage = instance_of_model_two.avatar.url
instance_of_model_one.save

Passing the URL instead of the object in the 1st statement resolved the issue.

Pushp Raj Saurabh
  • 1,174
  • 12
  • 16