Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ruby copy a paperclip attachment from one model to another?

Tags:

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

like image 824
Gary Avatar asked Jan 08 '13 21:01

Gary


2 Answers

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
like image 142
cih Avatar answered Sep 18 '22 03:09

cih


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

It worked for me.

like image 24
scc Avatar answered Sep 20 '22 03:09

scc