Recreate_versions! using the unique file name method specified on the wiki

I am trying to use method recreate_versions! , but I use the wiki method to create unique file names. The problem is that when you run recreate_versions! it changes the file names, but does not update them on the mounted object itself. How can I update these URLs?

+2
ruby rubygems carrierwave
source share
2 answers

Here is what worked for me. It uses the file name if it already exists. Therefore, they do not change when you recreate version!

 def filename if original_filename if model && model.read_attribute(:avatar).present? #or whatever you call your column model.read_attribute(:avatar) else # create new filename however you're doing it end end end 
+3
source share

The solution that works when working with caching is to save the mounted object after reconstructing the versions:

Example:

 avatar.image.recreate_versions! avatar.save! 

This way, you can continue to use unique file names even if you recreate versions and properly handle caching.

+9
source share

All Articles