Validates with :if

前端 未结 4 437
深忆病人
深忆病人 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:51

    I think you have an error in your syntax:

    validates :one, :two, :presence => true, :if => :condition_testing?
    
    def condition_testing?
        !(one == 0 && two == 1)
    end
    

    There was one :if too many in there... And if I understand correctly you want to have it only validate in case one == 0 && two == 1? Then you condition_testing? is inverted (leave out the !())

    If unsure you could try to use pry and insert a breakpoint into your condition_testing? method to see what's going on.

    (Please note added ":" before condition testing)

提交回复
热议问题