1 class Product
< ActiveRecord
::Base
3 has_many
:orders, :through => :line_items
5 validates_presence_of
:title, :description, :image_url
6 validates_numericality_of
:price
7 validate
:price_must_be_at_least_a_penny
8 validates_uniqueness_of
:title
9 validates_format_of
:image_url,
10 :with => %r
{\
.(gif
|jpg
|png
)$
}i
,
11 :message => "must be a URL for GIF, JPG, or PNG image."
13 def self.find_products_for_sale
15 :conditions => "date_available <= now() and (date_available_until is null or date_available_until >= now())",
20 def price_must_be_at_least_a_penny
21 errors
.add(:price, 'should be at least 0.01') if price
.nil? || price
< 0.01