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.
The problem is that you're using a presence validator with a condition that checks the values of the attributes. This is incorrect. A presence validator checks to make sure those attributes are set. What's worse, you're passing the if
option (@Tigraine was correct about your syntax being wrong, by the way), which means whenever that method returns true, the presence won't be checked at all. The way you have this set up, the validator will run only when one
is equal to 1 and two
is equal to 0. Otherwise, no validations are run at all! I think the best option here is to write a custom validation:
validates :one_and_two
def one_and_two
errors.add(:base, "one must be 1 and two must be 0") if !(one == 0 && two == 1)
end
This will add an error to the model with the specified message if the condition returns true. (Note: I'm still not clear on what condition is valid and which is invalid, so feel free to change that last part to suit your needs.)