Skip to content

Instantly share code, notes, and snippets.

@zenom
Last active October 24, 2022 23:59
Show Gist options
  • Save zenom/445bcce7f639fa5f4c45d63cf05da014 to your computer and use it in GitHub Desktop.
Save zenom/445bcce7f639fa5f4c45d63cf05da014 to your computer and use it in GitHub Desktop.
# 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