Why I got #Ecto.Association.NotLoaded?

后端 未结 1 1500
没有蜡笔的小新
没有蜡笔的小新 2021-02-13 05:56

I have teams and each team has users, so there is a join table to link users to teams as its a many to many relation, here is my models:

defmodule App.Team do
           


        
相关标签:
1条回答
  • 2021-02-13 06:22

    You need to preload the :user as well as the :team_users:

    teams_users =
      from(t in Team, where: t.owner_id == ^user_id)
      |> Repo.all()
      |> Repo.preload(team_users: :user)
    

    There is a section on nested associations in the docs. https://hexdocs.pm/ecto/Ecto.Query.html#preload/3

    0 讨论(0)
提交回复
热议问题