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."
10 def self.find_products_for_sale
12 :conditions => "date_available <= now() and (date_available_until is null or date_available_until >= now())",
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