bahalperin
bahalperin
Explore posts from servers
AEAsh Elixir
Created by bahalperin on 7/13/2023 in #support
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:
calculations do
calculate :unread_count,
:integer, expr(length(messages.channel_id == ^arg(:channel_id) && messages.created_at > current_member.last_read_at))
end
calculations do
calculate :unread_count,
:integer, expr(length(messages.channel_id == ^arg(:channel_id) && messages.created_at > current_member.last_read_at))
end
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
defmodule App.Chat.Channel do
relationships do
many_to_many :members, App.Account.User do
through App.Chat.ChannelMember
source_attribute_on_join_resource :channel_id
destination_attribute_on_join_resource :user_id
writable? true
filterable? true
end

has_many :messages, App.Chat.Message do
sort created_at: :desc
filterable? true
end
end
end
defmodule App.Chat.Channel do
relationships do
many_to_many :members, App.Account.User do
through App.Chat.ChannelMember
source_attribute_on_join_resource :channel_id
destination_attribute_on_join_resource :user_id
writable? true
filterable? true
end

has_many :messages, App.Chat.Message do
sort created_at: :desc
filterable? true
end
end
end
defmodule App.Chat.ChannelMember do
attributes do
attribute :channel_last_read_at, :utc_datetime_usec
end

relationships do
belongs_to :channel, App.Chat.Channel, primary_key?: true, allow_nil?: false
belongs_to :user, App.Account.User, primary_key?: true, allow_nil?: false
end
end
defmodule App.Chat.ChannelMember do
attributes do
attribute :channel_last_read_at, :utc_datetime_usec
end

relationships do
belongs_to :channel, App.Chat.Channel, primary_key?: true, allow_nil?: false
belongs_to :user, App.Account.User, primary_key?: true, allow_nil?: false
end
end
defmodule App.Chat.Message do
attributes do
create_timestamp :created_at
end

relationships do
belongs_to :channel, App.Chat.Channel do
allow_nil? false
attribute_writable? true
end
end
end
defmodule App.Chat.Message do
attributes do
create_timestamp :created_at
end

relationships do
belongs_to :channel, App.Chat.Channel do
allow_nil? false
attribute_writable? true
end
end
end
11 replies