4

How can I verify a paperclip attachment does not exist if another field does exist? I tried:

validates_attachment :img, presence: false, if: :some_other_field?
def some_other_field?
  some_other_field
end
lwiseman
  • 750
  • 7
  • 29
  • This is what I settled on, but I'm still looking for a more elegant way: `validates :img_file_name, absence: true, if: :some_other_field?` – lwiseman Aug 26 '13 at 23:19

3 Answers3

2

Similar problem here, my solution was to make the comparison in the def

validate :check_image_with_title 

def check_image_with_title
   if !ctitle.blank? and cimage.blank?
      #If ctitle IS NOT empty and cimage IS empty, add a custom error message
      errors.add :key, "You need an image to go with your title"
      return false
    else
      return true
   end 
end
Steve Root
  • 368
  • 3
  • 13
1

Try this:-

validates_attachment :img, presence: true, if: :some_other_field?
def some_other_field?
  some_other_field.present?
end
techvineet
  • 5,041
  • 2
  • 30
  • 28
0

Have you tried to use exists? instead present?, could be works

validates_attachment :img, presence: false, if: :some_other_field?
def some_other_field?
  some_other_field.exists?
end