class Participant < ApplicationRecord # Associations belongs_to :employer, optional: false has_many :employments has_many :workers, through: :employments has_many :service_contracts has_many :vendors, through: :service_contracts has_many :employer_records has_many :employers, through: :employer_records has_and_belongs_to_many :programs has_many :bank_accounts, as: :owner # Validations validates :first_name, :last_name, :ssn, presence: true validates :ssn, uniqueness: { message: "SSN is already taken by another participant" }, unless: :ssn_already_taken_by_employer? validates :employer_id, presence: true def full_name "#{first_name} #{last_name}" end private def ssn_already_taken_by_employer? # Check if the SSN already exists in the Employer model, excluding the current participant's employer Employer.where.not(id: employer_id).exists?(ssn: ssn) end end