Skip to content

Instantly share code, notes, and snippets.

@alpaca-tc
Created May 23, 2021 13:17
Show Gist options
  • Save alpaca-tc/aa691507766dfde2474a4f2896df0bb7 to your computer and use it in GitHub Desktop.
Save alpaca-tc/aa691507766dfde2474a4f2896df0bb7 to your computer and use it in GitHub Desktop.
値がtrue/falseのいずれかであることをテスト
# validates :column_name, boolean: true のように使う
class BooleanValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
return if value.is_a?(TrueClass) || value.is_a?(FalseClass)
record.errors.add(attribute, :not_boolean)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment