Conditional filters in read actions
Hi there, I'm back with another quesion that i couldnt find an answer to - sorry if i missed something.
I have a resource with a list of form submissions, where i have a read action to get all form submission of a specific form id, with the form_id as an argument. that works fine.
Now i also want to add the possibility to get all records submitted after a specific date, which also works this way:
filter expr(submission_time >= ^arg(:min_submission_time))
.
Now, the :min_submission_time
argument is optional - i want to be able to use the same action for getting both all as well as the filtered submissions. Is there a way to have a conditional in the action to only apply the :min_submission_time
filter if the argument is not nil? As far as I know the action code is evaluated at compile time, so i cant use a normal elixir if/else.
I am aware that i could just split this up into 2 different actions, but that would require duplicating things like the default sort order and filtering by form id.
Thanks!6 Replies
You have to options π the built in filter option is really just short hand for adding a custom preparation that filters.
The other option is that ash expressions support
if
Thanks! I tried the second solution but found the following
- if I keep the
else true
in it, then it seems to ignore other filters in the action
- if i remove the else true
then i always get 0 results
thats how it looks now:
OK, my bad it always ignores the first filter - seems like i can only have one filter.
I can combine both filters like this - is that the "idiomatic" syntax for this?
Nope, that wonβt work.
That would always return everything. Youβd combine them into a single expression with
or
Or and
But yeah there can currently only be one filter. Many people have asked for there to be multiple so I should probably just do that today πWell but when i think of it, if there are multiple filters then its unclear if they are AND or OR π so there would need to be a new syntax
so you mean like this?
cleaner like this, seems to work - thanks!! π
when I support that, it will be
and
similarly to how calling query |> Ash.Query.filter(...) |> Ash.Query.filter(...)
will use and
but Yeah that looks great πgreat, thanks again for your help!