josefrichter

josefrichter

I have schema a Class has_many User through Enrollment. Currently I have a single enrollments join table, that holds additional data like processed_at parameter.

I am displaying to a user a list of his classes, and need to display his rank (in fact row_number) in each class, which is derived from processed_at parameter. i.e. something like “you are 5th in class Foo, and 7th in class Bar”.

In my live view I am grabbing the user and his enrollments separately:

current_user = Accounts.get_user_by_session_token(session["user_token"])
current_user_enrollments = Accounts.get_user_enrollments(current_user.id)

the first one is standard phoenix auth function, the second one is the more tricky one:

def get_user_enrollments(user_id) do
    enrollments_with_rank =
      from e in Enrollment,
      select: e,
      # this partitions e by class_id and assigns row_number within that partition
      select_merge: %{rank: over(row_number(), partition_by: e.class_id, order_by: e.processed_at)}

    user_enrollments =
      from ex in subquery(enrollments_with_rank),
      where: ex.user_id == ^user_id

    Repo.all(user_enrollments)

  end

I can’t seem to figure out a syntax that would allow me to do this within a preload. This is a function I tried to create:

def preload_ranked_enrollments(user) do
    enrollments_with_rank =
      from e in Enrollment,
      select: e,
      select_merge: %{rank: over(row_number(), partition_by: e.class_id, order_by: e.processed_at)} # assigns rank by processed_at

    user
    |> Repo.preload(enrollments: enrollments_with_rank)
    |> Repo.preload(enrollments: [:class])
  end

and then call it from liveview this way:

current_user = Accounts.get_user_by_session_token(session["user_token"])
    |> Accounts.preload_ranked_enrollments()

However, it seems like the query in preload_ranked_enrollments actually somehow applies where: e.user_id == ^(user.id) because it always returns all enrollments with rank 1. Meaning if I got enrollments with given class_id and user_id, it would always return just 1 row and obviously give it rank 1. Is this something that Repo.preload actually does? Does it somehow inject that additional where clause into the query?

Note: I use terms “rank” and “row_number” interchangeably here, even though in postgres these are two different things and I am aware of the difference. I initially used the term “rank” in my schema, but getting row_number (always unique) rather than rank from postgres. Will probably change this to avoid confusion in code in future.

Thank you.

Showing Posts 1 to 2

fuelen

fuelen

yes, you can inspect this in logs.
Try to wrap enrollments_with_rank in preload_ranked_enrollments function into a subquery, as you did in your first example.

josefrichter

josefrichter OP

Thank you for the suggestion, it seems like that works:

def preload_ranked_enrollments(user) do
    enrollments_with_rank =
      from e in Enrollment,
        select: e,
        # assigns rank by processed_at
        select_merge: %{
          rank: over(row_number(), partition_by: e.class_id, order_by: e.processed_at)
        }

    user_enrollments =
      from ex in subquery(enrollments_with_rank),
        where: ex.user_id == ^(user.id)

    user
    |> Repo.preload(enrollments: user_enrollments)
    |> Repo.preload(enrollments: [:class])
  end
— All posts loaded —

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
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
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

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
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews