benonymus

benonymus

Ecto query for list of items' ids

Hey I am returning a list of users from a function, and I want to use that list for a query.
how could I use the ids of those items for the query selection?

  def get_recording_list_by(team_members) do
recording_query =
  from(
    r in Recording,
    preload: [:user],
    where: r.user_id == ^team_members.user.id, --- questionable part
    select: r
  )

Repo.all(recording_query)
end

Marked As Solved

benonymus

benonymus

fixed it by using in instead of ==
so the final code looks like this:

def get_recording_list_by_team_id(team_members) do
    IO.inspect(team_members)
    team_members_ids = Enum.map(team_members, fn team_member -> team_member.id end)
    IO.inspect(team_members_ids)

    recording_query =
      from(
        r in Recording,
        preload: [:user],
        where: r.user_id in ^team_members_ids,
        select: r
      )

    Repo.all(recording_query)
  end

Also Liked

LostKobrakai

LostKobrakai

Or just recording_query = from Ecto.assoc(team_members, :recording), preload: [:user]

peerreynders

peerreynders

https://hexdocs.pm/ecto/Ecto.Query.API.html#in/2

PostgreSQL IN comparison

though you’re possibly talking about from p in Post

https://hexdocs.pm/ecto/Ecto.Query.html#from/2

In general the in expression is looking for an Ecto.Queryable on the right hand side - if it’s a string it is referring to a DB table. The Ecto.Queryable is usually a module that uses Ecto.Schema

alexandre

alexandre

Can anybody point to me where this IN is documented in ecto? I cannot find, and it took me so long to find this post :frowning:

benonymus

benonymus

I’m glad my post helped you as well as it helped me :grin:

Where Next?

Popular in Questions Top

logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
Kagamiiiii
Student & New to elixir. Nice language. I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
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
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
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
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call 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
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
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
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
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

We're in Beta

About us Mission Statement