Validates with :if

前端 未结 4 472
深忆病人
深忆病人 2021-01-02 01:07

I\'m trying to create a condition in which the attribute \'one\' is zero and the attribute \'two\' is one, then a model is not valid. But when I make:

Model.         


        
4条回答
  •  被撕碎了的回忆
    2021-01-02 01:59

    You are better off using numericality and equal to.

    validates :one, :numericality => { :equal_to => 0 }
    
    validates :two, :numericality => { :equal_to => 1 }
    

    http://guides.rubyonrails.org/active_record_validations_callbacks.html#numericality

提交回复
热议问题