Last active
October 24, 2022 23:59
-
-
Save zenom/445bcce7f639fa5f4c45d63cf05da014 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# A user can be a part of multiple teams and orgs. The OrgTeam | |
# table has user_id, organization_id and team_id | |
defmodule Conspyre.Customers do | |
import Ecto.Query | |
alias Conspyre.Repo | |
alias Conspyre.Customers.{Team, OrgTeam} | |
alias Conspyre.Users.User | |
def teams_for_user(current_user) do | |
team_query = from t in Team, select: %{id: t.id, name: t.name} | |
user_query = from u in User, preload: [teams: ^team_query] | |
user_info = Repo.one(user_query) | |
user_info.teams | |
end | |
end | |
## OR ## | |
def teams_for_user(current_user) do | |
team_data = | |
Repo.get(User, current_user.id) | |
|> Repo.preload(:teams) | |
team_data.teams | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment