2024-01-16 20:54:44 -06:00
|
|
|
class Employer < ApplicationRecord
|
2024-02-05 15:58:46 -06:00
|
|
|
# Direct association with participants
|
|
|
|
has_many :direct_participants, class_name: 'Participant'
|
2024-02-02 23:12:36 -06:00
|
|
|
|
2024-02-05 15:58:46 -06:00
|
|
|
# Association through EmployerRecord
|
|
|
|
has_many :employer_records
|
|
|
|
has_many :indirect_participants, through: :employer_records, source: :participant
|
2024-01-29 22:04:56 -06:00
|
|
|
|
2024-02-05 15:58:46 -06:00
|
|
|
# Association with Workers through direct_participants
|
|
|
|
has_many :workers, through: :direct_participants
|
|
|
|
|
|
|
|
# Other methods...
|
2024-02-05 16:18:06 -06:00
|
|
|
def full_name
|
|
|
|
"#{first_name} #{last_name}"
|
|
|
|
end
|
2024-02-06 00:24:38 -06:00
|
|
|
|
|
|
|
|
2024-02-07 05:13:12 -06:00
|
|
|
class Employer < ApplicationRecord
|
|
|
|
# Associations and other methods as before...
|
2024-02-06 00:24:38 -06:00
|
|
|
|
2024-02-07 05:13:12 -06:00
|
|
|
validate :unique_as_participant, if: -> { ssn_changed? || new_record? }
|
|
|
|
validates :ssn, uniqueness: { allow_blank: true }, if: -> { ssn.present? }
|
|
|
|
validates :tin, uniqueness: { allow_blank: true }, if: -> { tin.present? }
|
2024-02-06 00:24:38 -06:00
|
|
|
|
2024-02-07 05:13:12 -06:00
|
|
|
private
|
|
|
|
|
|
|
|
def unique_as_participant
|
|
|
|
if Participant.exists?(ssn: ssn) && (id.blank? || Participant.where.not(id: id).exists?(ssn: ssn))
|
|
|
|
errors.add(:ssn, 'A participant with the same SSN already exists')
|
2024-02-06 00:24:38 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2024-02-07 05:13:12 -06:00
|
|
|
end
|
|
|
|
|