How do I know if an image is uploaded or not? - paper clip

Do you know if there is a way to find out if an image is uploaded?

I mean, I have a Foo_Class, and this class may have an attached image, but its presence is optional. Is there a way to find out if a particular instance of this class has an image or not?

Thanks!

+8
ruby-on-rails-3 paperclip
source share
5 answers

When you added Paperclip to your model, you added certain paperclip lines, mine

cover_file_name cover_content_type cover_file_size cover_updated_at 

Then I check if it is zero or not

  Foo_Class.cover_file_name.nil? 
+8
source share

If foo.image? returns true, then the file is downloaded.

+12
source share

I think the correct solution is to use the file? method file? .

foo.image.file?

http://rdoc.info/github/thoughtbot/paperclip/Paperclip/Attachment#file%3F-instance_method

used exist? will execute a server request to check if there is a file that can be quite slow, especially if it is on another server or on S3.

using foo.image_file_name.nil? probably matches the file? under the covers, but ou doesn't want to depend on the implementation of a paper clip that might someday change.

+6
source share

If it is in my model

 has_attached_file :avatar, :styles => {:logo => "230x50>", :card_image => "180x50>"} 

You can check if image is uploaded for user ie @user

 <%= @user.avatar.exists? %> 

This will return a boolean value.

+1
source share

Suppose you want to check for an attachment (image) for the 1st row in the Foo model:

 image_present = Foo.first.image? 

This returns true if the application is present.

0
source share

All Articles