diff options
author | HombreLaser <sebastian-440@live.com> | 2023-04-06 14:22:22 -0600 |
---|---|---|
committer | HombreLaser <sebastian-440@live.com> | 2023-04-06 14:22:22 -0600 |
commit | fc191f53503629087fd453e626ec2a4c58e0a21b (patch) | |
tree | aa6fa09f35b62cc302245832e0995a274de32a0a /app/models/product_cart.rb | |
parent | b7455b581022059a87633864c2ac2751b035e1e1 (diff) |
AƱadidos validadores
Diffstat (limited to 'app/models/product_cart.rb')
-rw-r--r-- | app/models/product_cart.rb | 9 |
1 files changed, 9 insertions, 0 deletions
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 |