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
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
Jump to solution
6 Replies
Solution
barnabasj
barnabasj•4w ago
I would probably go with a has_one with the from_many and sort options set
Songyun
SongyunOP•4w ago
would that be used like the below?
has_one :stat, TitleStat do
sort inserted_at: :desc
from_many? true
end
has_one :stat, TitleStat do
sort inserted_at: :desc
from_many? true
end
and then
calculate :latest_word_count, :integer, :stat do
stat.word_count
end
calculate :latest_word_count, :integer, :stat do
stat.word_count
end
barnabasj
barnabasj•4w ago
The calculation would be
calculate :latest_word_count, :integer, expr(stat.word_count)
calculate :latest_word_count, :integer, expr(stat.word_count)
Songyun
SongyunOP•4w ago
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???
relationships do
has_many :title_stats, RegVision.Gov.TitleStat do
sort inserted_at: :desc
end

has_one :stat, RegVision.Gov.TitleStat do
sort inserted_at: :desc
from_many? true
end
end


read :with_stat do
prepare build(load: [:stat])
filter expr(exists(stat, true))
end
relationships do
has_many :title_stats, RegVision.Gov.TitleStat do
sort inserted_at: :desc
end

has_one :stat, RegVision.Gov.TitleStat do
sort inserted_at: :desc
from_many? true
end
end


read :with_stat do
prepare build(load: [:stat])
filter expr(exists(stat, true))
end
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 redundant
barnabasj
barnabasj•4w ago
A 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
sevenseacat
sevenseacat•4w ago
The two relationships are totally independent so if you want them both sorted a certain way, you need to specify the sort on both

Did you find this page helpful?