How to use photo multiple times in paperclip and delete only when all instances are deleted
I want to give my user the ability to re-use photos they already uploaded in different pages and different orientations so I need to create multiple records for each photos in the database. I want though that if user delete one instance the photo stays until all instances are deleted开发者_运维百科 then delete the file.
What is the best way to do so in Paperclip?
If you only want a photo to be used in several pages but in no different ways, i.e. no different style configurations in paperclip, then you should only have one record for each photo and associate it to other records by a has_many :through association. That way you can let the middle model call the photo record upon destruction to see if there are any other associations left for the photo.
However, it is very difficult to be specific without seeing at least you current model structure and associations and some example of what your users will be allowed to do.
In any case, I would not recommend to actually have two separate paperclip records point to the exact same file on the file system.
Edit:
If it is the actual upload you want to avoid then you could always use the original file when creating the new record:
@first_photo = Photo.find(1)
@new_photo = Photo.new
@new_photo.attachment = @first_photo.attachment
@new_photo.save
The photo would still be stored once for every instance but the upload is avoided.
精彩评论