In my system, I have the following resources:
defmodule Core.Feedbacks.Student do
relationships do
alias Core.Feedbacks
has_many :feedbacks, Feedbacks.Feedback
many_to_many :classes, Feedbacks.Class do
through Feedbacks.StudentClass
join_relationship :student_classes
source_attribute_on_join_resource :student_id
destination_attribute_on_join_resource :class_id
end
end
end
defmodule Core.Feedbacks.Class do
relationships do
alias Core.Feedbacks
has_many :feedbacks, Feedbacks.Feedback
many_to_many :students, Feedbacks.Student do
through Feedbacks.StudentClass
source_attribute_on_join_resource :class_id
destination_attribute_on_join_resource :student_id
end
end
end
defmodule Core.Feedbacks.StudentClass do
relationships do
alias Core.Feedbacks.{Student, Class}
belongs_to :student, Student do
primary_key? true
allow_nil? false
attribute_writable? true
end
belongs_to :class, Class do
primary_key? true
allow_nil? false
attribute_writable? true
end
end
end
defmodule Core.Feedbacks.Feedback do
relationships do
alias Core.Feedbacks
belongs_to :student, Feedbacks.Student do
allow_nil? false
attribute_writable? true
end
belongs_to :class, Feedbacks.Class do
allow_nil? false
attribute_writable? true
end
end
end
As you can see, the Student
resource has a many_to_many
relationship with the Class
resource and both resources have a has_many
relationship with the Feedback
resource.
I wan´t to create an aggregate that will tell me how many feedbacks a student has inside a class.
If I just wanted all the student feedbacks, an aggregate like this in my Student
resource would work just fine:
count :total_feedbacks, :feedbacks
But I want to count all the feedbacks that are from that student and from an specific class, meaning that I would to, somehow, pass an :class_id
argument to inside that count aggregate filter expression.
I’m not sure how to solve that using aggregates