【发布时间】:2018-07-24 22:01:53
【问题描述】:
我正在尝试查看我当前用户的团队是否与传入的用户团队重叠。我有一些有用的东西,但我很好奇它是否能让我更有效率。这是我所拥有的:
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
然后我将它们与:
Enum.any?(user_teams, fn(t) -> t in current_user_teams end)
同样,这符合我的需求,但似乎有更好的方法来做到这一点?
【问题讨论】:
-
您是否在此请求的后面使用
user_teams或current_user_teams?如果是这样,我想您需要保持原样,但如果不是,您可以只执行一个查询,如果给定用户和当前用户都是团队的一部分,则只获取团队。
标签: elixir phoenix-framework ecto