sezaru
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
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m aware of 2 such projects, are there others?
git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
Hi all,
I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
How do I tell Hologram not to escape HTML when substituting into templates?
{Textile.render(@product.long_description)}
escapes the HTM...
New
I’d like to create a file with 600 permissions (rw-------), but there’s a race condition if I create it and then change the permissions. ...
New
Hi,
we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
When I use Mix.install with a git dependency, using git@github.com:... how do I change the SSH identity used?
I’ve got two Github accoun...
New
Other Trending Topics
Hey there,
I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first.
...
New
Hi everyone :waving_hand:
I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly)
AI dream has many faces, but general...
New
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #code-sync
- #discussion
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #blog-post
- #elixirconf-us
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #elixirconf-eu
- #processes
- #security
- #api
- #forms
- #performance











Showing Posts 1 to 5- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
zachdaniel
You can’t do that with standard aggregates, but you can do that with the new(-ish) inline aggregates syntax.
sezaru
As always, worked like a charm!
sezaru
Hey @zachdaniel, I have another case where I’m not 100% sure how to solve it.
The calculations with inline aggregates worked great, but what if I want to aggregate something from one of the relationships?
For example.
Let’s say that the
Classresource has ahas_manyrelationship with another resource calledLessons.I want to have a calculation in my
Studentresource that would give me number of lessons inside that class that a student is in.What I tried to do was to first create an aggregate in the
Classresource like this:And then, inside the
Studentresource, I tried to create a calculation using that:But that doesn’t work.
I also tried adding a calculation that would return the current class and then I would be able to get the
total_lessonsfield directly from it:But using the first aggregate doesn´t seem to be working, not sure if I’m doing something wrong with it.
zachdaniel
So, what I’m about to show you wont’ work, but what you would want is this:
However, you can’t use an aggregate as the
fieldof another aggregate. However, you can do this:Assuming that
lessonhasclass_idon it.sezaru
Yep, this worked great, thanks again!