better-restaurant-41251
02/19/2024, 3:20 AMClientResource has two belongs_to fields—`hospital` and `hospital_contact`—that are linked. That is, when a user is editing a Client and selects a hospital, the hospital_contact select should only show contacts associated with that hospital.
I've gotten part of the way there using attach_scope, like so:
ruby
field :hospital_contact,
as: :belongs_to,
attach_scope: -> { query.active.from_hospital(parent.hospital_id) },
required: false
This works when editing an existing Client, but falls over when the hospital is updated (but not saved) or when creating a new Client, because it's reading the hospital_id from the parent rather than getting it from the previous field.
Is there a way to modify the field's attach_scope based on the previous field's value? (I think if this were a select and not a belongs_to, I could do it with computed options?)loud-jewelry-99127
02/19/2024, 7:18 AMhospital_contact and connect that field to a stimulus controller that react on hospital change, fetching the values from a custom endpoint (passing the hospital PK as param) and repopulate the select on each change.
Let me know if you need some help, we have an example on our demo app on the courses form with the countries and cities
https://main.avodemo.com/avo/resources/courses/newlemon-wall-20836
02/19/2024, 11:04 AMlemon-wall-20836
02/19/2024, 11:05 AMlemon-wall-20836
02/19/2024, 11:05 AMlemon-wall-20836
02/19/2024, 11:05 AMcities select field