Allow Only Specific Words for Attribute

activerecord, attributes, ruby-on-rails

Solution

Add an inclusion validation to your model:

validates :animal, inclusion: { in: %w(dog cat bird fish) }

Problem

How do you restrict strings allowed in a string field to specific words? Example: I have a model attribute `animal:string` that I would like to exclusively accept `["dog", "cat", "bird", "fish"]`. Every thing else would make the object invalid.

Original source