0

I have a model with an enum like this:

enum health_company: {
    company_a: 0,
    company_b: 1,
    company_c: 2,
  }

and a validation that should allow health company "company_c" only if the job is of type "job_x":

validates :health_company, exclusion: { in: %i(company_c) }, unless: proc{ employee.last_job.job_x? }

This validation is not working. What's the error? Is it even possible to exclude certain values from an enum based on a condition?

Ronald
  • 68
  • 8
  • Hey I don't know how to post a linked question, but perhaps [this question will help you](https://stackoverflow.com/questions/8146965/how-do-i-specify-and-validate-an-enum-in-rails) – Lucas Luan de Melo Jan 13 '22 at 17:54

1 Answers1

0

You must use rescue_from ::ArgumentError in controller.

rescue_from ::ArgumentError do |_exception|
  render json: { message: _exception.message }, status: :bad_request
end
Hossein Safari
  • 252
  • 3
  • 15