CarrierWave file exists?

If you ever wondered how to check for file if exist while you were using carrierwave gem with storage on amazon S3 here is how.

Assuming you have User class:

class User < ActiveRecord::Base
  mount_uploader :avatar, AvatarUploader
end

and uploader class
class AvatarUploader < CarrierWave::Uploader::Base
  storage :fog
end

then only thing that you have to do is this

user = User.first
user.avatar.file.exists?

thats it. I kind of assumed that it should be simple avatar.exist? but you have to get fog instance of file to be able to access exists? method.

Author: Petr Zaparka

I'm a developer, but I think that communication is the most important aspect of product development. That's the reason why I'm focused on understanding how to talk to my peers and customer by improving my soft skills.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.