513625a985a539bc37dbe951f95bcdc8d026234a
[cs356-p2-videostore.git] / app / models / coitem.rb
1 class Coitem < ActiveRecord::Base
2   belongs_to :customer
3   belongs_to :rentable
4
5   # Make sure the user typed in the ids
6   validates_presence_of :customer_id
7   validates_presence_of :rentable_id
8
9   # Make sure the user typed in numbers
10   validates_numericality_of :customer_id
11   validates_numericality_of :rentable_id
12
13   # Make sure this rentable was not already checked out
14   validates_uniqueness_of :rentable_id
15
16   # Make sure the associated rentable and customer are valid and exist
17   validates_associated :customer
18   validates_associated :rentable
19
20   def overdue?
21     return due_date < Time.now.to_date
22   end
23
24   protected
25   def validate
26     errors.add(:customer_id, "does not exist is the database") if customer.nil?
27     errors.add(:rentable_id, "does not exist in the database") if rentable.nil?
28   end
29 end