End chapter 8
[depot.git] / app / models / product.rb
1 class Product < ActiveRecord::Base
2 validates_presence_of :title, :description, :image_url
3 validates_numericality_of :price
4 validate :price_must_be_at_least_a_penny
5 validates_uniqueness_of :title
6 validates_format_of :image_url,
7 :with => %r{\.(gif|jpg|png)$}i,
8 :message => "must be a URL for GIF, JPG, or PNG image."
9
10 def self.find_products_for_sale
11 find(:all,
12 :conditions => "date_available <= now()",
13 :order => :title)
14 end
15
16 protected
17 def price_must_be_at_least_a_penny
18 errors.add(:price, 'should be at least 0.01') if price.nil? || price < 0.01
19 end
20 end