Nicemaker

Nicemaker

Ecto Nested Preload Filter Referencing Top Level Assoc Join

I have a nested preload with a has_many relation and I would love to filter these references by a join I did on the top level, Here is my naive thinking:

 query =
      from vm in VariantMarket,as: :vm,
        left_join: m in assoc(vm, :market), as: :m,
        where: vm.id in ^ids,
        preload: [
          variant: [product: [market_settings: ^pms_query.()]]
        ]

and for pms_query I was hoping I could us the dynamic helper like this:

 pms_query = fn ->
     market_filter = dynamic([pm: pm, m: m], m.id == pm.market_id)
     from pm in Pdex.Catalog.ProductMarket,
        as: :pm,
        where: ^market_filter
    end

But I get ann error saying

unknown bind name `:m` in query

As far as I understand this, it’s not possible to reference any top-level joins within a preload. I also tried subqueries with parent_as in the preload part but that was also unsuccessful.

I know I could solve this particular case with a few more joins on the product assoc and then the market_setting assoc, which I could then reference directly in the preload.

But this is just an example to test this kind of filtering. I have a more complex example where multiple joins would be a nightmare.

I get the impression it’s, in general, not possible to simply use a top-level reference as a filter in a preload. Maybe because of the order of interpolation.

But maybe I am just not understanding the dynamic helper at all, and someone could give me a hand, or could at least confirm my assumption.

Thanks in advance

Bjorn

Where Next?

Popular in Questions Top

jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
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
hariharasudhan94
I would like to know what is the best IDE for elixir development?
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
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
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
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
New

Other popular topics Top

lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
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
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
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New

We're in Beta

About us Mission Statement