dobrygin

dobrygin

Nested association preloads doesn't work

Hello! I have structure: Post → Author and Post → Shared → Author. But my nested associations for Author inside Shared doesn’t work.

I got error: ** (KeyError) key :author not found in: #Ecto.Association.NotLoaded<association :shared is not loaded>

Here is my code:

def list_posts_by_author(params) do
{stamp, “”} = Integer.parse(params[“stamp”])
author = params[“author_id”]

stamp = 
  with {:ok, stamp} <- DateTime.from_unix(stamp, :millisecond) do
    DateTime.to_naive(stamp) else err -> err
  end

(from p in Post, 

  preload: [shared: :author],
  preload: [:author],
  where: p.inserted_at < ^stamp,
  where: p.author_id == ^author) 

  |> order_by(desc: :inserted_at)
  |> Repo.all()

end

Thank you!

First Post!

kokolegorille

kokolegorille

Welcome, You should show the schemas You are using to have a better answer. In particular Shared schema…

Last Post!

dobrygin

dobrygin

Yes, im getting error in view, but because of not preloaded association.

Where Next?

Popular in Questions Top

joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New

Other popular topics Top

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
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
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
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New

We're in Beta

About us Mission Statement