# 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