Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

109
Views
ActiveRecord::RecordInvalid Exception: Validation failed: "Something" must exist

I am adding all is needed to create a post, but am getting an error message saying "Facility must exist." For some reason, the facility_id is capturing the name of the facility and not the ID. I am setting state for facilities, and mapping / filtering for my dropdown. Please see below:

function AddPostForm({ posts, handlePost})
{
  const [facilities, setFacilities] = useState ([])
  const [procedures, setProcedures] = useState ([])
  const uniques = procedures.map(procedure => procedure.procedure)
  .filter((value, index, self) => self.indexOf(value) === index )
// console.log("unique procedures:", uniques)
  const uniqFacility = facilities.map(facility => facility.name).filter((value,index, self) => self.indexOf(value) === index)

  useEffect(() => {
    fetch(`/posts/${procedures}`)
      .then((r) => r.json())
      .then(setProcedures);
  }, []);
  useEffect(() => {
    fetch("/facilities")
      .then((r) => r.json())
      .then(setFacilities);
  }, []);

  const [formData, setFormData] = useState({
    facility_id: "",
    procedure:'',
    date_of_procedure:'',
    date_of_invoice:'',
    patient_cost: "",
    insurance_cost: "",
    comments: ""
  })

  const { id } = useParams();

  function handleChange(event) {
    setFormData({
      ...formData,[event.target.name]: event.target.value,
    });
  }

  function handleSubmit(event) {
    event.preventDefault();
    handlePost(formData)

return (
    <div >
       <form onSubmit={handleSubmit}>
       <label htmlFor="facility_id">Facility:</label>
      <select
        id="facility_id"
        name="facility_id"
        value={formData.facility_id}
        onChange={handleChange}      >
        <option value="">Select Facility</option>
        {uniqFacility.map((facility) => (
          <option key={facility.id} value={facility.name}>
            {facility}
          </option>
        ))}
      </select>
 </form>
    </div>
  );
}
export default AddPostForm;
When I check params in the byebug I see facility_id=>"Name of Facility", and get an error message saying that Facility must exist. I tried adjusting the map / filter function to ...facility => facility.id).filter ... , which ends up giving me the facility_id and creates the post as if the name of the entity was its id. I think I need to adjust the map/filter formula, but I do not know how. Appreciate if someone help me out here.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!