How to extract first element from `many_to_many`
I have a
many_to_many relationship and i want to get the first element from it. I have also a has_many relationship and can get the first element using has_one with from_many? but this is only the "merging" Resource. Is there something similar for many_to_many ?
I can't use calculations or aggregations, because they can't return a resource so has_one seems to be the only one left.5 Replies
Modelling
through relationships with Ash | sevenseacat.netThey're not natively supported, but you can still make them!
You can use a
has_one with a filter using parentThis works, when I use the
has_many as the basis inside of the filter and ignore the many_from_many
I am a bit confused though how to sort the has_many since it is the "merging" resource.
I found this in the docs
In the example from the website how would get largest city by population that is stored inside of the city if the parent is a "merging" tableI was thinking something along the lines of
The sort is a bit tricky, and I'm not 100% sure if it compiles, not on a machine with elixir atm
Thank you!
I will try it out
Also i saw in another chat that you can do something like this:
maybe there is a way to stuff similar thing into the
has_many or into calc