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
Document
:has_many :stats
:has_many :stats
, and I want to load the accompanying, most recent
Stat
Stat
only (for each
Document
Document
) whenever I fetch a
Document
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
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
The Elixir backend framework for unparalleled productivity. Declarative tools that let you stop wasting time. Use with Phoenix LiveView or build APIs in minutes for your front-end of choice.