Set Validation in Ruby

Today I was trying to figure out how to validate if a user entered a number in a given set. I searched the internet high and low and could only figure out how to validate a range. I actually found the solution in a book, so I thought I would post it.

class Story < ActiveRecord::Base
  validates_presence_of :name, :description
  validates_inclusion_of :storypoints, :in=>[1,2,3,5,8,13], :
          message =>"Story Points must be 1,2,3,5,8 or 13"
  belongs_to :sprint
end

Trackback URL

No Comments on "Set Validation in Ruby"

Hi Stranger, leave a comment:

ALLOWED XHTML TAGS:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Subscribe to Comments