31

Let's say my model has an image with :thumb and the client wants :tiny and :nano thumbnails.

How do I reprocess all the existing images using a rake task?

I've found a rake task that I thought would do it https://gist.github.com/777788 but it's giving me errors.

Joseph Le Brech
  • 6,541
  • 11
  • 49
  • 84

2 Answers2

57

According to the Carrerwave documentation you can use following commands:

Model.all.each do |model|
  model.image.recreate_versions!
end
Mikhail Nikalyukin
  • 11,867
  • 1
  • 46
  • 70
  • 1
    I've put that in a rake task now, it's a bit hard coded to my app but it'll do the trick – Joseph Le Brech Jan 30 '12 at 15:10
  • will this recreate EVERYTHING, or just the newer versions? – meow Jan 14 '13 at 06:07
  • 1
    It will recreate everything, if you need to recreate let's say images for last 100 records -> `Model.last(100).each { |m| m.image.recreate_versions! }` – Mikhail Nikalyukin Jan 14 '13 at 07:30
  • 2
    Depending on the storage, you may need to do this: https://github.com/carrierwaveuploader/carrierwave/wiki/How-to%3A-Recreate-and-reprocess-your-files-stored-on-fog – etagwerker Jan 23 '14 at 12:49
  • 3
    To get this to work, I had to add `model.save` inside the loop (which is also done by the code @etagwerker pointed to). – bjnord Mar 30 '14 at 12:05
  • @bjnord So true. At least on some versions of carrierwave or it might be Mongoid related, calling `recreate_versions!` does reprocess the images, but when you reload the model it references the old updated to reference these new files if you do not explicitly call save on the model. – Timo Apr 04 '14 at 05:29
  • 1
    I added the check: `model.image.recreate_versions! if model.image?` since not all objects have uploaded images in my case. – Besi Mar 05 '15 at 18:58
2

I wanted to expand on this great answer by Mikhail Nikalyukin

To Reprocess a single version you can do something like this

Model.all.each do |model|
  model.image.recreate_versions!(:version1, :version2)
end

this way if you added a new version you dont have to do all of them again

MZaragoza
  • 10,108
  • 9
  • 71
  • 116