Answer the question
In order to leave comments, you need to log in
Validation in Rails, how to query an adjacent field?
I carry out validation in the model of the entered data:
#app/models/validate.rb
validates :email, :presence=> true,
:format=> VALID_EMAIL_REGEX,
:uniqueness=> true
SELECT 1 FROM `subscriptions` WHERE (`subscriptions`.`email` = BINARY '[email protected]') LIMIT 1
SELECT 1 FROM `subscriptions` WHERE (`subscriptions`.`email` = BINARY '[email protected]') AND (`subscriptions`.'subscribe' = '1') LIMIT 1
Answer the question
In order to leave comments, you need to log in
I don't quite understand what you want to do. But to get the required request, do this:
So the validation will only run if the subscribe field is set to true. If you also need to check that subscribe is always true, then there is this option:
I don’t really like it. It is better to think about a custom validator or validation method .
"and you need to
SELECT 1 FROM `subscriptions` WHERE (`subscriptions`.`email` = BINARY '[email protected]') AND (`subscriptions`.'subscribe' = '1') LIMIT 1" - the request is invalid, because that there can be two soaps in the database, one with the subscription turned on, one with the subscription turned off.
And about validation, I agree with ibub1ik - it's better to make it a separate class.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question