Ruby on Rails - validates inclusion of an attribute in ruby on rails- ruby on rails tutorial - rails guides - rails tutorial - ruby rails
Example
- We 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
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team
- To check if a value is not included in an array.
- Using the exclusion:.
class User < ApplicationRecord
validates :name, exclusion: { in: %w(admin administrator owner) }
end