Example of setting a has_one relationship
I would appreciate an example of setting up a has_one relationship.
I have the following resources:
Each resource has an attribute
uuid_primary_key :id
.
I want to establish a 1:1 relationship between the two resources.
So, I add the following to App.Api.Item
:
I get the following errors/warnings:
Do I need to define an attribute :item_id
in resource App.Api.Variant
? If so, what type would it be?
Or am I on the wrong path?7 Replies
Yes, I need to also define the following in
App.Api.Variant
:
I think in
App.Api.Variant
you have to define a belongs_to
as supposed to adding attribute :item_id, :uuid
, so something like:
Yeah defining the relationship is the more idiomatic way.
Thank you @Jmanda!
And is there a way to recursively
Api.load!
all relationships, aggregates and calculations on a record that belongs_to
records that belongs_to
other records?You can take a look at https://ash-hq.org/docs/module/ash/latest/ash-query#function-load-2 for loading nested relationships
Thanks, I do that now already, I was wondering if there's a function to load everything that's nested.
That’s not realistic really because you’d typically have “cycles”
I.e a relationship back to the parent.