Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save leandromarques/31889a66e0cbd7db81cde1d5ebe13cb3 to your computer and use it in GitHub Desktop.
Save leandromarques/31889a66e0cbd7db81cde1d5ebe13cb3 to your computer and use it in GitHub Desktop.
Conditional validation example in Rails
validates :datetime_is_not_in_past, if: :datetime_not_nil?
def datetime_is_not_in_past
errors.add(:datetime_field_name, :invalid, message: "cannot be in the past") if self.datetime_field_name < DateTime.current
end
def datetime_not_nil?
!self.datetime_field_name.nil?
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment