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.
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
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
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
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
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
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
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
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
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
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
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
ICal is a library for interacting with iCalendar data. It parses iCalendars into typed Elixir structs via ICal.from_ics, and can prepare ...
New
Latest Phoenix Threads
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
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #ai
- #phoenix_html
- #elixirconf-us
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
idi527
You have a typo in the query. Should probably be
where: u.id == ^user_id.idi527
I think that’s how table joins work. In
psqlyou would get multiple rows. Maybe tryRepo.preloadorEcto.Query.preload.saint
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
It might be. Depends on the amount of data that you query.
A join or
Ecto.Query.preloadmight 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
Okay thanks sir for the info
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
There are different ways to use it. I think it’s best to check out the documentation:
Best regards
aphelion00
Already tried with the examples in documentation, all ended with Ecto.QueryErrors.
joaquinalcerro
Oh…
Can you share more details of what you are trying to do?
benwilson512
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.