How do I validate that two values do not equal each other in a Rails model?

前端 未结 9 1652
既然无缘
既然无缘 2020-12-29 20:31

I have a User model, which has an email and a password field. For security, these may not be equal to each other. How can I define this in my model?

9条回答
  •  天命终不由人
    2020-12-29 21:12

    more fun:

      validates :password, exclusion: { in: ->(person) { [person.email] }, message: "cannot use protected password" }
    

    which might be even better in this case, since you could check for other forbidden values with something like

      validates :password, exclusion: { in: ->(person) { [person.email, person.first_name, person.last_name, person.phone_number, person.department_name] }, message: "cannot use protected password" }
    

提交回复
热议问题