saint

saint

def get_all_post_by_user(user_id) do
  query = from u in User,
     where: u.id == ^user_id,
      join: p in Post, on: p.user_id == u.id,
      select: %{
          user_id: u.id,
          username: u.username,
           title: p.title,
           description: p.description
      }
   query |> Repo.all
end

i have this query but when i try to render it, it returns multiple set of data but same User with different Post.
like this.
[

              %{user_id: 1,
              username: "saint",
               title: "test1",
               description: "sample1"},

              %{user_id: 1,
              username: "saint",
               title: "test2",
               description: "sample2"}
]

Thanks in advance.

Showing Posts 1 to 10

idi527

idi527

You have a typo in the query. Should probably be where: u.id == ^user_id.

idi527

idi527

I think that’s how table joins work. In psql you would get multiple rows. Maybe try Repo.preload or Ecto.Query.preload.

saint

saint OP

Sorry for the typo and thanks for the quick response, ahm. i try to avoid using preload because of this https://medium.com/@alves.lcs/phoenix-ecto-preload-can-be-evil-96f877fb52f5. is it true? thanks

idi527

idi527

is it true?

It might be. Depends on the amount of data that you query.

A join or Ecto.Query.preload might be evil as well since then ecto would have to parse multiple almost identical rows like in your example and then put them all into some “parent” struct. Repo.preload, which fires a second query, might be more efficient in that case.

saint

saint OP

Okay thanks sir for the info :slight_smile:

aphelion00

aphelion00

Can someone post an example of how to use preload to achieve this? I’m very new and haven’t wrapped my mind around it yet.

joaquinalcerro

joaquinalcerro

There are different ways to use it. I think it’s best to check out the documentation:

Best regards

aphelion00

aphelion00

Already tried with the examples in documentation, all ended with Ecto.QueryErrors.

joaquinalcerro

joaquinalcerro

Oh…

Can you share more details of what you are trying to do?

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Welcome! We’re happy to help. In order for that to be possible though, it’s important that you provide full and complete information showing the code you tried, and the errors you got.

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
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
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
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
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
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

Other Trending Topics Top

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
aseigo
ICal is a library for interacting with iCalendar data. It parses iCalendars into typed Elixir structs via ICal.from_ics, and can prepare ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews