diff --git a/app/controllers/participants_controller.rb b/app/controllers/participants_controller.rb index acc3865..1d113fa 100644 --- a/app/controllers/participants_controller.rb +++ b/app/controllers/participants_controller.rb @@ -33,15 +33,17 @@ class ParticipantsController < ApplicationController if @participant.save redirect_to @participant, notice: 'Participant was successfully created.' else - render :new + flash.now[:alert] = 'There was an error creating the participant.' + render :new, status: :unprocessable_entity end end rescue ActiveRecord::RecordInvalid => e flash.now[:alert] = "Error: #{e.message}" - render :new + render :new, status: :unprocessable_entity end + def edit end diff --git a/app/models/employer.rb b/app/models/employer.rb index 0799c41..b7c96b1 100644 --- a/app/models/employer.rb +++ b/app/models/employer.rb @@ -16,6 +16,8 @@ class Employer < ApplicationRecord validate :unique_as_participant + validates :ssn, uniqueness: true, allow_blank: true, presence: true + validates :tin, uniqueness: true, allow_blank: true, presence: true private diff --git a/app/models/participant.rb b/app/models/participant.rb index 0cf49b2..b6f6a0e 100644 --- a/app/models/participant.rb +++ b/app/models/participant.rb @@ -7,15 +7,21 @@ class Participant < ApplicationRecord has_many :vendors, through: :service_contracts has_many :employer_records has_many :employers, through: :employer_records - + has_and_belongs_to_many :programs # Validations - validates :first_name, presence: true - validates :last_name, presence: true + validates :first_name, :last_name, :ssn, presence: true + validate :ssn_must_be_unique_across_models + def full_name "#{first_name} #{last_name}" end - # Other associations - has_and_belongs_to_many :programs -end + private + + def ssn_must_be_unique_across_models + if Participant.where.not(id: id).exists?(ssn: ssn) || Employer.exists?(ssn: ssn) + errors.add(:ssn, 'SSN is already taken by another participant or employer') + end + end +end \ No newline at end of file diff --git a/app/models/vendor.rb b/app/models/vendor.rb index 381a29c..398ce6a 100644 --- a/app/models/vendor.rb +++ b/app/models/vendor.rb @@ -3,4 +3,7 @@ class Vendor < ApplicationRecord has_and_belongs_to_many :employers has_many :service_contracts has_many :participants, through: :service_contracts + + # Validations + validates :tin, uniqueness: true, allow_blank: true, presence: true end diff --git a/app/models/worker.rb b/app/models/worker.rb index f124b89..d861cbb 100644 --- a/app/models/worker.rb +++ b/app/models/worker.rb @@ -12,6 +12,7 @@ class Worker < ApplicationRecord # Validations validates :first_name, presence: true validates :last_name, presence: true + validates :ssn, uniqueness: true, allow_blank: true, presence: true # Method to return the full name of the worker def full_name diff --git a/app/views/participants/_form.html.erb b/app/views/participants/_form.html.erb index 9e84ea6..833035c 100644 --- a/app/views/participants/_form.html.erb +++ b/app/views/participants/_form.html.erb @@ -1,14 +1,14 @@ <%= form_with(model: participant, local: true, html: { class: 'needs-validation', novalidate: true }) do |form| %> - <% if participant.errors.any? %> -