summaryrefslogtreecommitdiff
path: root/app/models/product_cart.rb
blob: 09e7a25fb0e278fbd6660f89c37bed4c39bcc7fb (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.nil? || cart.products.find_by(id: product_id).nil?

    errors.add(:product_id, "cart already has product with id #{product_id}")
  end
end