RoR: model validation issue

I have a basic ActiveRecord model in which I have two fields that I would like to test. The requirement is that at least one of the fields must have a value. Both can make a difference, but at least one needs a value.

How to express it with

validates_presence_of 

expression? For instance:

 validates_presence_of :main_file validates_presence_of :alt_file 

I do not want an error to occur if only one of them is empty, only if both of them are empty.

+4
source share
2 answers
 validates_presence_of :main_file, :if => Proc.new { |p| p.alt_file.blank? } validates_presence_of :alt_file, :if => Proc.new { |p| p.main_file.blank? } 
+5
source

change .nil? to .blank? doing the trick!

+3
source

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


All Articles