# Rails style subset validation

DevFeed: [Rails style subset validation](<https://devfeed.tech/articles/rails-style-subset-validation-21029.md>)

Original publisher: [Read original article](<https://jakeyesbeck.com/2015/06/21/validates-subset/>)

Published: 2015-06-21T12:00:00Z

Content type: tutorial

Language: en

Sources: [Jake Yesbeck](<https://devfeed.tech/sources/jake-yesbeck.md>)

Topics: [Rails](<https://devfeed.tech/topics/rails.md>), [Ruby](<https://devfeed.tech/topics/ruby.md>), [data](<https://devfeed.tech/topics/data.md>), [Framework](<https://devfeed.tech/topics/framework.md>), [Code](<https://devfeed.tech/topics/code.md>)

Tags: [activerecord](<https://devfeed.tech/tags/activerecord.md>), [examples](<https://devfeed.tech/tags/examples.md>), [library](<https://devfeed.tech/tags/library.md>), [rails](<https://devfeed.tech/tags/rails.md>), [ruby](<https://devfeed.tech/tags/ruby.md>), [terminal](<https://devfeed.tech/tags/terminal.md>), [validation](<https://devfeed.tech/tags/validation.md>)

## AI overview

This tutorial introduces a Rails-style subset validator for validating that a collection contains only permitted values. It explains the data-validation rationale, shows installation and usage of the validates_subset library in Rails and other Ruby frameworks, and demonstrates valid, empty, invalid, and non-set inputs along with supported validation modifiers.

## Source excerpt

Do you suddenly wake up in a cold sweat, wondering if there is finally a way to validate that your data is a proper subset that you desire? I too have experienced this horror, and that is why I made a rails style subset validator. Validations über alles Data validation is important. Having data you can depend on is the difference between a great application and one that might be classified as: "amateur hour". Rails provides a nice amount of validators via ActiveModel::Validations, but not an exhaustive set (get it?). One of those missing pieces involves validating sets of data. Just in case you haven't had your coffee yet, we define a set as an abstract data type that stores distinct values in no particular order. Ruby does indeed have the Set class but most code I've come across simply uses the Array class. So, for simplicity's sake, all examples will use Array. Now that we have all been convinced that subset validation is the greatest thing since sliced bread, how about some examples? Example usage To include validates_subset, simply add gem 'validates_subset' in your Gemfile of your projects. Or type gem install validates_subset in your terminal. For rails applications, the gem is automagically available to you. For other ruby frameworks, just add the line: require 'validates_subset' wherever you need the library. For this, we can assume that our application is using Rails 4.0 and we have a class that looks like: class HasASubset < ActiveRecord::Base attr_accessor :foo validates_subset :foo, [1, 2, 3] end Using our handy dandy test class, we can see that: A valid subset is valid: test = HasASubset.new test.foo = [1] test.valid? # => true An empty set is also very valid: test = HasASubset.new test.foo = [] test.valid? # => true An invalid set is... invalid: test = HasASubset.new test.foo = ['banana'] test.valid? # => false And a non-set is invalid: test = HasASubset.new test.foo = 99_999 test.valid? # => false Holy guacamole that is some sweet validation. But wait,