Calculations Using Relationships
I'm writing a simple chat app to try to learn Ash. I have models like below and I'd like to write an
:unread_count
calculation on the Channel
. It would take the current user as the actor and find the associated member of the channel, use that :last_read_at
time to filter down the messages associated with the channel to ones that has a :created_at
after that time, and count them.
I'm having a hard time figuring out how to do that based on the examples.
What I think I want is something like:
Assuming :current_member
is another calculation that exists to get the member associated with the current user.
Apologies if I'm thinking about this in the completely wrong way and thanks in advance for the help!
Models
4 Replies
You're thinking about it in the right way, but this might be one that requires dipping down into SQL actually
There are a few ways you can go about it
You can do it in a separate query like so:
Theres a lot to unpack in that calculation 😆
What might be easier is adding calculation args
Do you need to be able to filter/sort on this value?
Thanks for the quick response! Nope, was just going to display it next to the name of the channel, like slack. When you say "adding calculation args" do you mean like having multiple separate calculations that take arguments? Like getting all the unread messages given a timestamp? And then do more of the work in separate steps outside of one calculation? Like load current channel member -> get unread messages given their last read timestamp?
In that case, I'd suggest using a module-based calculation 🙂
You can run an ecto query or any other thing to get the data you want that way
Just making sure I'm looking at the right thing, this is what you're talking about? https://ash-hq.org/docs/guides/ash/latest/topics/calculations#module-calculations. Think I missed those in the docs before. Thank you!!