calculations do
calculate :for_patio, :boolean, expr(exists(patios, id == ^arg(:id)) do
argument :id, :uuid, allow_nil?: false
end
calculate :for_parceiro, :boolean, expr(exists(parceiros_precos_estadias, parceiro_id == ^arg(:parceiro_id) and voucher == ^arg(:voucher)))
end
And then you can query like
query =
Resource
|> Ash.Query.filter(for_parceiro(parceiro_id: parceiro_id, voucher: voucher))
# or
query =
Resource
|> Ash.Query.filter(for_patio(patio_id: patio_id))
# or combine them
query =
Resource
|> Ash.Query.filter(for_parceiro(parceiro_id: parceiro_id, voucher: voucher) and for_patio(patio_id: patio_id))
# they can also be combined in ways you can't combine your example
query =
Resource
|> Ash.Query.filter(for_parceiro(parceiro_id: parceiro_id, voucher: voucher) or for_patio(patio_id: patio_id))
You cold then call a code interface and provide this query
Thanks @zachdaniel. With all this explanation I can move on. I think I get stucked 'cause I feel I have to put all queries inside actions. As far as I can see, it’s not true. Many examples you give show queries that don’t rely on actions, right? In this case, should I create a regular module and put there the queries inside regular functions?
If you are considering making a module somewhere with functions like:
def published_posts do
Post.read!(query: Ash.Query.filter(published == true)
end
Then at that point I would suggest making it an action, i.e
read :published do
filter expr(published == true)
end
If you need caller-level composition, then calculations are a good choice.
I know it would be nice if there was “one right answer” but in these case it often comes down to what kind of interface you would need. The general rule of thumb though is: when in doubt, put stuff in an action that does specifically what you need.
No problem I appreciate your questions, and I while we definitely want to improve the documentation, having these kinds of questions here in the forum will help other people when they have similar questions. Documentation improvements are a longer term type thing, but this will help people in the short term
Hey @tellesleandro i just wanna say that your questions and the ensuing discussion is very helpful for newbies like me. Thanks for asking these questions!
Welcome @yasoob. I’m developing real world application for real world customers, not only CRUD examples. So, expect to find more and more newbie questions from me here.