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

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 55125 245
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
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44778 311
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement