Read actions

I have this read action:
read :groups_by_organization_id do
argument :organization_id, :uuid, allow_nil?: false

filter expr(organization_id == ^arg(:organization_id))
pagination offset?: true, countable: true, default_limit: 1000
end
read :groups_by_organization_id do
argument :organization_id, :uuid, allow_nil?: false

filter expr(organization_id == ^arg(:organization_id))
pagination offset?: true, countable: true, default_limit: 1000
end
Inside the filter expression, instead of the incoming organization_id, I want to use a different value that I derive from it:
decendant_orgs = get_descendant_organizations!([organization_id])
ids = [arg(:organization_id)] ++ Enum.map(descendant_orgs, org -> org.id end)

filter expr(organization_id == ^ids)
decendant_orgs = get_descendant_organizations!([organization_id])
ids = [arg(:organization_id)] ++ Enum.map(descendant_orgs, org -> org.id end)

filter expr(organization_id == ^ids)
Is this possible?
6 Replies
ZachDaniel
ZachDaniel2w ago
prepare fn query, _ ->
Ash.Query.before_action(query, fn query ->
organization_id = query.arguments.organization_id
descendants = get_descendant_organizations!([organization_id])
ids = [organization_id | descendants]
Ash.Query.filter(query, organization_id in ^ids)
end)
end
prepare fn query, _ ->
Ash.Query.before_action(query, fn query ->
organization_id = query.arguments.organization_id
descendants = get_descendant_organizations!([organization_id])
ids = [organization_id | descendants]
Ash.Query.filter(query, organization_id in ^ids)
end)
end
you can use a preparation w/ a before_action hook to run more queries that modify the main query
Ege
EgeOP2w ago
I tried that but I got:
The pin operator ^ is supported only inside matches or inside custom macros. Make sure you are inside a match or all necessary macros have been required

36 │ Ash.Query.filter(query, organization_id in ^ids)
The pin operator ^ is supported only inside matches or inside custom macros. Make sure you are inside a match or all necessary macros have been required

36 │ Ash.Query.filter(query, organization_id in ^ids)
ZachDaniel
ZachDaniel2w ago
add require Ash.Query to the top of your module
ZachDaniel
ZachDaniel2w ago
GitHub
Better error message for undefined variables within remote calls to...
Current behavior When calling macros like Ecto.Query, you often end up with an error message that can be cryptic, especially with beginners. For example, the following code without adding require E...
ZachDaniel
ZachDaniel2w ago
I have an issue out for this kind of error being confusing
Ege
EgeOP2w ago
Thanks, this appears to be working 🙂

Did you find this page helpful?