Search code examples
ruby-on-railsmodel-view-controllerruby-on-rails-4carrierwavejquery-file-upload

carrier wave uploaded image should not exist but does in rails


I have a model called "Profile" and one of the attributes is called "pic". In the console, when I do:

User.first.profile

The following is returned.

 => #<Profile id: 64, created_at: "2014-06-19 15:59:41", 
      updated_at: "2014-06-19 15:59:41", user_id: 24, 
      description: "fds aslkjd aslkfjhas dlkjashdf lajkdshf al...", 
      rate: nil, pic: nil >

As you can see, the "pic" attribute is nil because I never uploaded a picture. I am trying to write several "if statements" to distinguish whether or not the picture exists. For example:

 @profile = User.find(24).profile.first
if @profile.pic

I've also tried:

@profile = User.find(24).profile.first
if @profile.pic.exists?

All of these evaluate to true, which should not happen since a picture has never been uploaded.

I've also tried:

@profile = User.find(24).profile.first
if @profile.pic.nil?

and for some reason that evaluates to false.

When I run in the console:

=> #<PicUploader:0x007ff8abeb8560 @model=#<Profile id: 64, 
     created_at: "2014-06-19 15:59:41", updated_at: "2014-06-19 15:59:41", 
     user_id: 24, description: "fds aslkjd aslkfjhas dlkjashdf lajkdshf al...", 
     rate: nil, pic: nil>, @mounted_as=:pic> 

I don't know where that is coming from. How do I write an "if statement" for whether or not the image exists that will actually return false like it should?

Thanks.


Solution

  • With carrierwave, you should use either the question-method of your attribute, pic?, or you can use blank?/present?:

    @profile.pic.present?
    # => false
    @profile.pic?
    # => false
    @profile.pic.blank?
    # => true