Sakthibalan
Fetch unique users with distinct query
How to fetch unique user by ‘user_id’ a query with distinct
Resources:
-
Ash.Domain.User
-
Ash.Domain.Project
has_many :milestones -
Ash.Domain.Milestone
belongs_to :project
has_many :tasks -
Ash.Domain.Task
belongs_to :milestone
has_one :task_assignee -
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
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{}`
1
Popular in Questions
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
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
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
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
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
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
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
Phoenix 1.4.0 released
Phoenix 1.4 is out! This release ships with exciting new features, most notably
with HTTP2 support, improved deve...
New
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
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
New
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
After calling mix ecto.create I get this error:
17:00:32.162 [error] GenServer #PID<0.412.0> terminating
** (Postgrex.Error) FATAL...
New
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









