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

前端 未结 9 1678
既然无缘
既然无缘 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:10

    Create custom validataion:

    validate :check_email_and_password
    
    def check_email_and_password
      errors.add(:password, "can't be the same as email") if email == password
    end
    

    But keep in mind that storing password as a plain text is bad idea. You should store it hashed. Try some authentication plugin like authlogic or Restful authentication.

提交回复
热议问题