Validates inclusion of an attribute

suggest change

You 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

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents