Alex F
Joined
10 Experience
0 Lessons Completed
0 Questions Solved
Activity
Hey Mitja, this post is a bit old now but maybe I can help the next person looking for this information :)
The way I've dealt with this exact same situation was with a job (ActiveJob or Sidekiq or whatever you choose to use). On the model that has the photo attachment, I have a callback like this:
after_commit :process_images, if: :photos_attached?
process_images simply calls a job: ProcessImagesJob.perform_later(self)
ProcessImagesJob looks something like this. I removed my app specific code, but hopefully you'll get the idea:
photos.each do |photo|
next unless photo&.representable?
preview_variant = photo.representation(resize_to_limit: [300, 300])
large_variant = photo.representation(resize_to_limit: [1000, 1000])
preview_variant.processed
large_variant.processed
end
This generates the variant files on my storage provider, so they're ready to be used when the user views the image. Hope that helps!