No Such Relationship
I have a many_to_many relationship between Plan and Post. Plan is the source, the relationship is defined there. Post is the destination. PlansPosts is the join resource. It's all in the same api this time. I'm making a form with, and calling manage_relationship from, Post, and that's resulting in a Spark error:
I based this relationship on my existing, working, cross-api many_to_many SitesUsers, and I've run through the relationship guide a few more times to check if I'm missing anything from recent updates. Nothing is jumping out at me yet.
Am I meant to be able to use relationships in actions on destination resources and not just source resources? That's the only difference that immediately comes to mind compared to the SitesUsers relationship that works.
This error persists after
mix clean --all
, mix deps.update ash
(still using main
), etc10 Replies
🤔
manage_relationship(:plans)
requires a :plans
relationship.
On the resource in question
otherwise what would it be managing?Okay, so, right, currently I /think/ that relationship is being defined from the parent/source? Should I also define it in the destination which is where the action is? I may be misunderstanding how this is intended to work structurally
plan.ex
post.ex
(correct me if i have committed a grave conceptual faux pas)
Yeah, so you can only manage a relationship on the current resource
So do you not have a
:plans
relationship on Post
?
Relationships are not bi-directional in this way. i.e one resource doesn't know that another relationship points at it
so having a posts
relationship on Plan
wouldn't create a :plans
relationship on Post
(a good thing to ask there is how would we know to call it :plans
?)Right. I defined the relationships on Plan because I was thinking it's intended to be a central source that many things hook into, like Site is, but whereas Site manages its child resources directly, you wouldn't go to the Plan area to manage posts or anything else that is sort of filtered by that plan
clarifying my mental model
So if I define a corresponding many_to_many on Post using the same join resource and everything, will that be okay with Spark? Or should I be rethinking the architecture?
yeah that would be perfectly fine (in fact it is what typically happens for many to many relationships)
Just make sure to flip your
source_attribute_on_join_resource
and destination_attribute_on_join_resource
I considered this briefly and then assumed an inflector came into play 😅
Gotcha, yeah we have enough magic without bringing wordplay into it 😆
okay, NICE. i want to do what is typical and best practices etc, the golden path 😬
Definitely standard to define relationships on both respective resources, but only if you need it for something
so don't feel obligated to define every single relationship
(cakephp brain)
got it, I think i grok a little better what they're for now, ash is about explicit modeling, the relationships are needed on the resources that use them in their actions, there isn't an invisible handwavy land of bidirectional inference
That's much more clear! Shallow water!