blob: eb9cf9fa9ca675dedcd65bf7a7fb77690e091301 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
# frozen_string_literal: true
# ProductCart
# quantity: integer
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
|