Ash FrameworkAF
Ash Framework5mo ago
13 replies
Songyun

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--

calculations do
  calculation :latest_word_count, expr(first(:stats).word_count))
end
Solution
I would probably go with a has_one with the from_many and sort options set
Was this page helpful?