So I'm looking at this link but I'm wondering how to do an 'OR' instead of an 'XOR'. Does anyone know how to do this? I am trying to do this using Rails 3.
2 Answers
There is short option for Rails 4 (not sure about Rails 3) :
validates :email, presence: { if: -> { username.blank? } }
validates :username, presence: { if: -> { email.blank? } }
-
1The OP's answer is better than this one from a UX point of view. With this approach, if both fields are left blank the user is shown two slightly misleading errors: "email can't be blank" and "username can't be blank". Better to show a single error explaining at least one of them can't be blank.– MatthewApr 9 at 4:00
I figured it out. all you have to change is the ^ to &&
private
def time_or_money
if time.blank? && money.blank?
errors[:base] << "Specify Time, Money or Both."
end
end