Ruby on Rails ActiveRecord Validations Validates inclusion of an attribute


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) }

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) }