lundi 22 février 2016

Ruby on Rails check that multiple values aren't blank or equal to each other

I have a form in a ruby on rails site that takes in a bunch of different parameters. I want to ensure that none of these parameters equal each other IF they are not blank. With how the form is designed, none of these values are required, so some of them can be blank. I can do this with a conditional statement of course, but I wanted to ensure whether or not there is a cleaner way to do so as the conditional statement would be HUGE with all of those checks. Again, if any of the values are equal to each other when (and only when) those values are chosen, I want to return an error that they can not be equal. The values are chosen by collection_select controls, so I'm not seeing any way to filter out duplicates beforehand. Is there a more concise way to compare all of these values?

if ((value1 == value2) && !value2.blank?) || 
   ((value1 == value3) && !value3.blank?) ||
   ((value1 == value4) && !value4.blank?) ||
   .... 
   ((value9 == value10) && !value10.blank?))
    Do error stuff
else
    Behave normally
end

Aucun commentaire:

Enregistrer un commentaire