Ruby on Rails ActiveRecord Validations Validates inclusion of an attribute


Example

You can check if a value is included in an array using the inclusion: helper. The :in option and its alias, :within show the set of acceptable values.

class Country < ApplicationRecord
  validates :continent, inclusion: { in: %w(Africa Antartica Asia Australia
                                            Europe North America South America) }
end

To check if a value is not included in an array, use the exclusion: helper

class User < ApplicationRecord
  validates :name, exclusion: { in: %w(admin administrator owner) }
end