From fc191f53503629087fd453e626ec2a4c58e0a21b Mon Sep 17 00:00:00 2001 From: HombreLaser Date: Thu, 6 Apr 2023 14:22:22 -0600 Subject: AƱadidos validadores MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/product_cart.rb | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'app/models/product_cart.rb') diff --git a/app/models/product_cart.rb b/app/models/product_cart.rb index 10d4a4c..eb9cf9f 100644 --- a/app/models/product_cart.rb +++ b/app/models/product_cart.rb @@ -5,4 +5,13 @@ class ProductCart < ApplicationRecord belongs_to :cart belongs_to :product + + validates :quantity, presence: true, comparison: { greater_than: 0 } + validate :sole_product_in_cart + + def sole_product_in_cart + return if cart.products.find_by(id: product_id).nil? + + errors.add(:product_id, "cart already has product with id #{product_id}") + end end -- cgit v1.2.3