Using manage_relationship with nested relationships
I have three resource modules:
With this in place, I was able to figure out how to insert patients into the
database pretty easily, however my problems arose when trying to do the exams.
The exams are loaded from an outside data source where we derive the event series
based on a date value. So what I tried to do was:
When doing this, an error is returned saying that the event series is invalid
because the patient relationship is not defined. Is it possible to do this with
a call to
manage_relationship
or am I fundamentally missing something?
Thanks so much for any assistance, Ash is very cool so far!12 Replies
So you have two options. To do it the way you're doing it, you can just add
attribute_writable?: true
to the patient
relationship.
And actually that is how I'd suggest that you do it, so lets just leave it at the first option.
With that said, I'd suggest defining a custom action for this logic, and putting it in the resource.
Then you can do Exam |> Ash.Changeset.for_create(:create, %{performed_on: performed_on, event_series: %{...}}) |> Api.create()
Thanks so much! I tried the first option and that solved my problem for the meantime. I'm going to probably refactor the code to use the custom actions, though, since that looks cleaner for what I'm ultimately trying to accomplish. thanks for answering so quickly and for the hard work on Ash!
Generally speaking, you should prefer to put as much of your business logic behind your resources as possible, as they are meant to be more "domain objects" than "database objects" (although the two intersect often).
That makes sense, I'll try to keep that in mind. It's a different way compared to what I'm used to but I can definitely see the usefulness of it
I just encountered another hiccup. I updated the code slightly:
(note that I removed the
custom_indexes
block and replaced it with an identies
block).
I changed the changeset logic somewhat to include the identity:
It is possible for multiple exams to exist in the same series, so I was hoping it'd look up the event series in the database first, but it doesn't seem to be doing that. Is this something I can change or should I just create the series as a separate action at this point? Just trying to wrap my head around it. Thanks!What should happen if it looks it up?
I was hoping it'd use the event series that it found in the database
That is what
append_and_remove
should dohm, strange... I might be doing something wrong. I see some errors about a violated uniqueness constraint in those cases where the series already exists in the database
lol
so we should be giving you an error for this
initital_date
I guess we're allowing an identity
with an invalid field 😢hmm, like my resource is messed up or the data is invalid?
no there is a typo there
inititial_date -> initial_date
lol woops. ok good catch. I should probably call it a night, it looks like 🙂
working like a charm now. thanks again!