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

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

    all you need is to create validation rule in your model for example

    class User < ActiveRecord::Base
      def validate_on_create
        if email == password
          errors.add("password", "email and password can't be the same")
        end
      end
    end
    

提交回复
热议问题