Paper clip conditional check

How to verify that a paperclip application does not exist if another field exists? I tried:

validates_attachment :img, presence: false, if: :some_other_field? def some_other_field? some_other_field end 
+4
source share
3 answers

A similar problem here, my solution was to make a comparison in 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 
+2
source

Try the following: -

 validates_attachment :img, presence: true, if: :some_other_field? def some_other_field? some_other_field.present? end 
+1
source

Have you tried using exists? instead of present? could work

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

Source: https://habr.com/ru/post/1498958/


All Articles