# frozen_string_literals: true module Jobshop class RFQ < ApplicationRecord self.primary_keys = %i[ organization_id rfq_id ] after_initialize { self.rfq_id ||= SecureRandom.uuid if new_record? } after_create { reload } belongs_to :organization, inverse_of: :rfqs belongs_to :customer, inverse_of: :rfqs, optional: true, foreign_key: %i[ organization_id customer_id ] belongs_to :requested_by, class_name: "Jobshop::Customer::Contact", primary_key: %i[ organization_id customer_id contact_id ], foreign_key: %i[ organization_id customer_id requested_by_id ], optional: true has_many :rfq_lines, inverse_of: :rfq, foreign_key: %i[ organization_id rfq_id ] def customer=(value) customer_id = (Jobshop::Customer === value) ? value.customer_id : value write_attribute(:customer_id, customer_id) end end end