jjabba

jjabba

Unable to understand docs around many-to-many ecto preload functions

Im trying to understand this section of the ecto documentation:

https://hexdocs.pm/ecto/Ecto.Query.html#preload/3-preload-functions

For many_to_many - the function receives the IDs of the parent association and it must return a tuple with the parent id as the first element and the association map or struct as the second. For example, if a post has many tags, when preloading the tags with a custom function, the function will receive a list of "post_ids" as the argument and it must return a tuple in the format of {post_id, tag}

By experimentation I’ve figured out that parent here refers to the post itself. Which leads to my first question.

Why does Repo.preload(my_post, tags: &my_custom_preloader/1) call my custom preloader with an array containing the id of my_post? When would it ever get more Ids?

Now to the second source of my confusion: the function will receive a list of "post_ids" as the argument and it must return a tuple in the format of {post_id, tag}

How do I go about transforming many post ids to one post id and one tag if the result somehow is to represent a many-to-many relationship?

Most Liked

LostKobrakai

LostKobrakai

Repo.preload can be called with a list of posts.

jjabba

jjabba

That makes sense. Something like

Repo.all(Post) |> Repo.preload(tags: &my_custom_preloader/1)

but with that confirmed, I’m even more confused how to return something meaningful if it has to be a tuple in the format of {post_id, tag}.

Wouldn’t it make more sense to return a list of tuples?

Exadra37

Exadra37

Welcome to the club :slight_smile:

I also had some difficulty in understanding them but after watching some videos from @AlchemistCamp I was able to get my ahead around many_to_many relations in Ecto :slight_smile:

I am not able to find which video I used on the time, but he has now a beginner series on Ecto and you can watch his video on many_to_many associations to see if you can understand them better, and while doing so you can take some video-notes here:

Screenshot from 2021-04-20 21-18-31

You can also watch the video directly on his website by following the links in my notes.

Where Next?

Popular in Questions Top

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
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
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
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
makeitrein
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project. Baby step #1 is extracting the number ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

Harrisonl
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
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
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
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement