nallwhy

nallwhy

I’m trying to sum only the copies_sold of :studio albums using an aggregate, but I’m not sure how to apply a filter to the joined records.

Here’s a simplified version of my resource:

def AshPg.Music.Artist do
  ...

  aggregates do
    sum :studio_copies_sold, [:albums], :copies_sold do
      # Invalid reference albums.type
      # filter expr(albums.type == :studio)

      # Not working
      join_filter [:albums], expr(type == :studio)
    end
  end
end

And here’s a test that creates an artist with multiple albums, where only one is of type :studio. The aggregate should return 1_000_000, but currently it’s returning the total sum of all album types.

defmodule AshPg.Music.ArtistTest do
  ...

  test "solo_copies_sold" do
    artist =
      Music.create_artist!(%{
        name: "250",
        albums: [
          %{title: "Rear Window", type: :single, copies_sold: 1000},
          %{title: "Bang Bus", type: :single, copies_sold: 2000},
          %{title: "뽕", type: :studio, copies_sold: 1_000_000}
        ]
      })

    %{studio_copies_sold: studio_copies_sold} = artist |> Ash.load!([:studio_copies_sold])

    # The sum of copies sold for studio albums should be 1_000_000
    assert studio_copies_sold == 1_000_000
  end
end

join_filter doesn’t seem to have any effect.

https://github.com/nallwhy/ash_pg/commit/4a84d244554620cb4ff602e9dcaa12bcaccf591d

Is there a correct way to apply a filter to aggregates based on joined relationships? Any help would be appreciated.

Showing Posts 1 to 2

zachdaniel

zachdaniel

Creator of Ash

join_filter is for “intermediate” relationships, not the destination relationship.

    sum :studio_copies_sold, [:albums], :copies_sold do
      # Invalid reference albums.type
      # filter expr(albums.type == :studio)

      # Not working
      filter expr(type == :studio)
    end
nallwhy

nallwhy OP

Thanks — I completely misunderstood the code below.

https://github.com/ash-project/ash/blob/acd2e31d4ecc1602bc3c259db383d202ce90ad25/documentation/topics/resources/aggregates.md#L96-L109

  • filter expr(redeems.redeemed == true): I thought it was accessing resource → redeems.redeemed, but it’s actually going through dealredeems.redeemed.

  • join_filter [:redeems, :deal], expr(active == parent(require_active)): the join_filter is used to access parent, which makes sense.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews