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.

Leave a Reply

Your email address will not be published. Required fields are marked *

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