ruby copy a paperclip attachment from one model to another? ruby copy a paperclip attachment from one model to another? ruby ruby

ruby copy a paperclip attachment from one model to another?


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.avatarinstance_of_model_one.save


old_avatar = model1.avatar;model2.avatar.create(attachment: old_avatar.attachment);model2.save;

It worked for me.


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_imageplayer.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.urlplayer.save!

This shall do the work!