Bidirectional `manage_relationship`
Is there a way to get
manage_relationship
to manage the other side of the relationship simultaneously? Specifying destination_relationship: :other
etc.26 Replies
manage_relationship
calls actions on the destination resource, and by default those are the primary actions on the resource, but it can be changed by changing your options to manage_relationship
(see the docs in Ash.Changeset
module for manage_relationship
.
If you have those actions also call manage_relationship
then they can do what they wantOk, so if my primary create action on the destination resource does something like:
How does it know which argument to provide to the related resource's create action?
The input is provided nested
If the action on
relationship
managed :other
This looks like it's for nested relationships.
Ah, I guess I don't know what you mean about bidirectional then
Populating the reverse relationship on the related resource.
ah, I see. Nothing does that currently
You can add an
after_action
hook to do it manually if you wanted
That may or may not workCould you show me a real example of what operation you're trying to do?
and what you'd like to see returned
for example
Sure, sec
This is how I'm doing it now, which works:
With the
create
action above on the target resource.
Wondering if there's a shorthand to tell manage_relationsip
to populate the relationship on the destination resource with the current resource, e.g. by providing the relationship name on the destination resource, or specifying it in the relationships
section, e.g. reverse_relationship
.Nothing to do that kind of thing automatically. So on the other resource, you have a
manage_relationship
call also?
It seems like you'd just want to load that data, no need to manage itCurrently, yes, for this method.
I think
load
doesn't inject the current resource on the destination?thats what the
others: :self
does
using manage_relationship
at all relates the destination things to the current thing
you don't have to do that manually
it just doesn't load the reverse relationship automaticallyIf I don't add the current resource like this:
I was getting
nil
in the reverse relationship on the other resource.where were you getting
nil
? In the changeset?I'm not too worried about it loading the reverse relationship, just creating it.
In the resulting created resources.
🤔 you were getting
nil
, not %Ash.NotLoaded{}
?Querying them after creation, the reverse relationship was
nil
.Sounds like potentially a misconfiguration of your relationship fields or something like that
To debug further I think I'd need you to reproduce it in a test in ash
So this should just work the way I was thinking, populating the current resource on the destination relationship during creation?
yep!
Thats the main purpose of
manage_relationship
is to create/update/destroy related things (and so creating them would imply making them related on creation)
I'd check to make sure the reverse relationship isusing the right keysGreat, I had the impression that this was how it was supposed to work 🙂
OOC is it required that there is an
argument
+ manage_relationship
call in the default create action of the destination resource?Nope
@\ ឵឵឵ did you ever figure this one out?
@Zach Daniel I believe so, but I didn't have time to dig into what was going wrong earlier. Tbh the behavior you described was what I was expecting at the start. If it crops up again I'll put together a minimal.