How to load a limited amount of related records (and maybe store as virtual attr?)
Still pretty new to Ash and ran into some issues yesterday getting some data into my liveview. What I want to do is load a relationship when I fetch a collection of records, but only take the most recently inserted record. That would then either be accessed as something like a 'virtual field' on the main record or through the relationship (not sure what's best). How would I accomplish this?
E.g. a
Document
:has_many :stats
, and I want to load the accompanying, most recent Stat
only (for each Document
) whenever I fetch a Document
collection.
I tried something like the below but it was not valid--
6 Replies
Solution
would that be used like the below?
and then
The calculation would be
If i just try to
load: [:stat]
it goes into an infinite loop 🙃
I restarted my server and it chilled out 🤔
yeah this line makes things go crazy
prepare build(load: [:stat])
apparently a has_many
and has_one from_many
on the same resource is a circular relationship when trying to use prepare build
? ...but not circular if i make a non-default :read
action???
this worked
if anyone knows why prepare works in one context but not the other (default :read
) i'd love to know
I am also curious if the sort
is needed on the has_one
or if having the has_many
sorted already makes that redundantA PR with a reproduction would be nice. This shouldn't be happening, but I'm also not sure how exactly you made it do that
The two relationships are totally independent so if you want them both sorted a certain way, you need to specify the sort on both