Add searching to the games controller
[cs356-p2-videostore.git] / app / models / coitem.rb
index a6bc1ad..8513465 100644 (file)
@@ -2,11 +2,24 @@ class Coitem < ActiveRecord::Base
   belongs_to :customer
   belongs_to :rentable
 
+  # Make sure the user typed in the ids
   validates_presence_of :customer_id
   validates_presence_of :rentable_id
 
+  # Make sure the user typed in numbers
+  validates_numericality_of :customer_id
+  validates_numericality_of :rentable_id
+
+  # Make sure this rentable was not already checked out
   validates_uniqueness_of :rentable_id
 
+  # Make sure the associated rentable and customer are valid and exist
   validates_associated :customer
   validates_associated :rentable
+
+  protected
+  def validate
+    errors.add(:customer_id, "does not exist is the database") if customer.nil?
+    errors.add(:rentable_id, "does not exist in the database") if rentable.nil?
+  end
 end