I'm trying to see if my current user's teams overlap with the passed in user's teams. I have something that works but I'm curious if it could me more efficient. Here is what I have:
user_teams = from(
t in MyApp.Team,
left_join: a in assoc(t, :accounts),
where: p.owner_id == ^user.id or (a.user_id == ^user.id and t.id == a.project_id)
) |> Repo.all
current_user_teams = from(
t in MyApp.Team,
left_join: a in assoc(t, :accounts),
where: t.owner_id == ^current_user.id or (a.user_id == ^current_user.id and p.id == a.project_id)
) |> Repo.all
And then I compare them with:
Enum.any?(user_teams, fn(t) -> t in current_user_teams end)
Again, this suits my needs but seems like there is probably a better way to do this?
user_teams
orcurrent_user_teams
later in this request? If so, I guess you need to keep it how it is, but if not you could just do a single query that only gets teams if both the given user and the current user are part of the team. – Akiko