Skip to content

Instantly share code, notes, and snippets.

@eileencodes
Created September 28, 2011 16:31
Show Gist options
  • Save eileencodes/1248405 to your computer and use it in GitHub Desktop.
Save eileencodes/1248405 to your computer and use it in GitHub Desktop.
class Widget < ActiveRecord::Base
# initialized contstant for the validator
VALID_COLORS = %(red white blue)
belongs_to :box
has_many :categorizations
has_many :categories, :through => :categorizations
validates_presence_of :name
validates_length_of :name, :minimum => 3
validates_numericality_of :size
validates :color, :inclusion => { :in => VALID_COLORS,
:message => "can only be red white or blue" }
before_validation :downcase_color
def downcase_color
self.color.downcase!
end
def category_names
categories.map {|c| c.cat_name}.to_sentence
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment