Abramodj
Abramodj

Reputation: 5879

How to know if an image has been uploaded or not? - Paperclip

do you know if is there a method to know if the image has been uploaded?

I mean, i have a Foo_Class, and this class can have an attached image, but its presence is not necessary. Is there a way to know if a particular instance of that class have the image or not?

Thanks!

Upvotes: 8

Views: 7414

Answers (5)

Pushp Raj Saurabh
Pushp Raj Saurabh

Reputation: 1204

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

image_present = Foo.first.image?

This returns true if the attachment is present.

Upvotes: 0

Animesh
Animesh

Reputation: 1033

If this is in my model

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

You can check if the image is uploaded for a user i.e @user

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

This will return boolean value.

Upvotes: 1

tomf
tomf

Reputation: 449

I think that the proper solution is to use the file? method.

foo.image.file?

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

using exists? will do a request to the server to check if the file is there, which can be quite slow, especially if it's on a different server or on S3.

using foo.image_file_name.nil? is probably the same as file? under the covers, but ou don't want to dependant on the implementation of paperclip, which could someday change.

Upvotes: 6

Quaternion
Quaternion

Reputation: 174

If foo.image? returns true, then file uploaded.

Upvotes: 11

Nick
Nick

Reputation: 1325

When you added Paperclip to your model you added paperclip specific rows, mine are

cover_file_name
cover_content_type
cover_file_size
cover_updated_at

Then I check whether it is nil or not

 Foo_Class.cover_file_name.nil? 

Upvotes: 8

Related Questions