Oh right, ty! Updated and had to add to add this to the calculation to stop errors:
@impl true
def load(_query, _opts, _context) do
[:parent]
end
on the resource :path is defined as
calculations do
calculate :path,
{:array, :string},
ContentCove.Calculations.Path
end
However the values displayed dont seem correct since path is supposed to return an array of strings. In the admin ui i see single string values that are seemingly incorrect for each topic.
defmodule ContentCove.Calculations.Path do
use Ash.Resource.Calculation
alias ContentCove.Articles.Topic
require Ash.Query
@impl true
def calculate(topic, _, _) do
fetch_all_topics(topic)
|> Enum.map(fn {_, topic} -> to_string(topic.slug) end)
|> IO.inspect()
|> Enum.reverse()
end
@impl true
def load(_query, _opts, _context) do
[:parent]
end
defp fetch_all_topics(topics, acc \\ %{}) do
acc = Map.merge(acc, Map.new(topics, fn topic -> {topic.id, topic} end))
#dbg(acc)
topics
|> Enum.map(& &1.parent_id)
|> Enum.filter(fn parent_id ->
parent_id && !Map.has_key?(acc, parent_id)
end)
|> case do
[] ->
acc
unfetched_parent_ids ->
Topic
|> Ash.Query.filter(id in ^unfetched_parent_ids)
|> Ash.read!()
|> fetch_all_topics(acc)
end
end
end
Judging by the terminal output it looks like a single array is built and somehow each row in the admin ui is being given 1 value from the array i.e ["entertainment", "local-sport", "celebrities", "sport", "cricket"] that is the output from the inspect above in calculate.
If i hardcode ["test1", "test2"] to be returned from the calculate function in the admin ui it shows 1 value from the array for each row and only returns 2 rows instead of the correct number shown in the screenshot. Furthermore I though maybe its supposed to return an array of arrays for each row but that too only displays s single value in the admin for each row ![]()























