Sakthibalan

Sakthibalan

Fetch unique users with distinct query

How to fetch unique user by ‘user_id’ a query with distinct

Resources:

  1. Ash.Domain.User

  2. Ash.Domain.Project
    has_many :milestones

  3. Ash.Domain.Milestone
    belongs_to :project
    has_many :tasks

  4. Ash.Domain.Task
    belongs_to :milestone
    has_one :task_assignee

  5. Ash.Domain.TaskAssignee
    belongs_to :user
    belongs_to :task

How to fetch unique users from a project using a distinct query?

I tried this below query:
Project
|> Ash.Query.filter(name: “example”)
|> Ash.Query.load([milestones: [tasks: :task_assignee]])
|> Ash.Query.distinct([:user_id])
|> Ash.read!()

  • unknown error: :distinct

Most Liked

zachdaniel

zachdaniel

Creator of Ash

I think you’d have a lot better luck with something like:

User
|> Ash.Query.filter(exists(task_assignee.task.milestone.project, name == "example"))

than trying to load distinct users starting at Project. If you did want to do it, though, you’d need something like:

Project
|> Ash.Query.filter(name: “example”)
|> Ash.Query.load([milestones: [tasks: task_assignee: Ash.Query.distinct(TaskAssignee, :user_id)]])
|> Ash.Query.distinct([:user_id])
|> Ash.read!()
|> # extract all of the task assignees from this data structure, because you will get back a list of `%Project{}`

Where Next?

Popular in Questions Top

ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

Other popular topics Top

chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31525 112
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44167 214
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42576 114
New

We're in Beta

About us Mission Statement