Ruby Copy a Paperclip Attachment from One Model to Another

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.avatar
instance_of_model_one.save

How to copy a file using Paperclip

After some more messing around with paperclip, I figured it out. It's ridiculously simple to copy files!

# Stupid example method that just copies a user's profile pic to another user.
def copy_profile_picture(user_1, user_2)
user_2.picture = user_1.picture
user_2.save # Copied the picture and we're done!
end

This also works great with amazon s3. Sweet

Rails - How to Move a PaperClip Model to Another Model

I've just tried the following:

Given two models, with a picture attachment and their own Paperclip configurations, say: Gallery1 and Gallery2.

Given I had one object in Gallery1 with a picture already attached.

Make:

Gallery2.create(:picture => Gallery1.first.picture)

And it creates the picture properly resized with the constraints of Gallery2.



Related Topics



Leave a reply



Submit