bagasprstyadi

bagasprstyadi

Ecto Preload with Condition from the Main Query

Hi guys just wondering is it possible to preload but with a condition from the main query

Let’s say I have a model named Post. A Post has a tag which is a string. Then I want to query a specific Post for example Post with id = idA. While querying the Post I also want to preload all other Post which have a similar tag under a field called similar_posts in the query. So it looks something like this:

 %Post{
      id: "ID_A",
      tag: "TopicA",
      similar_posts: [
        %Post{
          id: "ID_B",
          tag: "topicA"
        },
        %Post{
          id: "ID_C",
          tag: "TopicA"
        }
      ]
    }

Let’s say I already created an association in the Post model:

has_many :similar_posts, MyApp.Post

When I tried

from(p in Post,
    preload: [similar_posts: ^from(pp in Post, 
         where: pp.id != p.id and
                pp.tag == p.tag, 
         order_by: pp.inserted_at
    )]
)

It error said that p is undefined. Can someone help me? Am I doing something wrong here or is it not possible and better to seperate the query itself after retrieving the result.

Most Liked

LostKobrakai

LostKobrakai

That’s not actually a requirement in ecto. Using the though option it can also be used for many to many relationships for example. It doesn’t necessitate an actual foreign key relationship on the db level.

If you don’t join the to be preloaded posts on the root query then the preload will happen in a separate query, which then of course doesn’t have access to p anymore. You can look at preload functions to build your query or as already mentioned join things on your root query.

Where Next?

Popular in Questions Top

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
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
aalberti333
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
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
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
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

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
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
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
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
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
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
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New

We're in Beta

About us Mission Statement