Validates inclusion of an attribute
suggest changeYou can check if a value is included in an array using the inclusion: helper. The :in option and its alias, :within, set the acceptable values.
class Country < ApplicationRecord
validates :continent, inclusion: { in: ["Africa", "Antarctica", "Asia", "Australia",
"Europe", "North America", "South America"] }
end
To check that a value is not included in an array, use the exclusion: helper:
class User < ApplicationRecord
validates :name, exclusion: { in: %w(admin administrator owner) }
end
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents