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

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
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
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
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

Other popular topics Top

grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52774 488
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
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
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement