AF
Ash Framework•4mo ago
Oliver

Is it possible to "pluck" a sibling using an expr?

I have resources - Group -> has_many formations - Formation (group_id, lithostrat_order) I would like to have a calc on formation that goes via parent group to select a formation with lithostrat_order + 1 I am almost there with:
calculations do
calculate :formation_above,
:struct,
expr(group.formations.lithostrat_order > parent(lithostrat_order)) do
constraints instance_of: __MODULE__
end
calculations do
calculate :formation_above,
:struct,
expr(group.formations.lithostrat_order > parent(lithostrat_order)) do
constraints instance_of: __MODULE__
end
But this will return more than one unless I am at the second to top one when resolving this 😄
Solution:
so I've done stuff like this before with a relationship. eg. this is for a set of records called OperationExecutions: ```elixir has_one :previous_execution, MODULE do no_attributes? true...
Jump to solution
4 Replies
Solution
Rebecca Le
Rebecca Le•4mo ago
so I've done stuff like this before with a relationship. eg. this is for a set of records called OperationExecutions:
has_one :previous_execution, __MODULE__ do
no_attributes? true
sort created_at: :desc

filter expr(
operation_version_id == parent(operation_version_id) and
created_at < parent(created_at)
)
end
has_one :previous_execution, __MODULE__ do
no_attributes? true
sort created_at: :desc

filter expr(
operation_version_id == parent(operation_version_id) and
created_at < parent(created_at)
)
end
I've been meaning to write up a blog post about tricks like this
Oliver
OliverOP•4mo ago
ah yes no attr is a much better approach
Rebecca Le
Rebecca Le•4mo ago
basically, no_attributes? true will unset all default filters to return all records, and then it gets scoped down to those with the same operation_version_id that were created earlier, and then the has_one/sort limit it to only the correct record
Oliver
OliverOP•4mo ago
I even have this trick active in my codebase just total brainfart that I was reaching for a calc 😂 thank you very much for reminding me

Did you find this page helpful?