在Rails AR中可以运行如下查询:
current_user.articles # fetch all articles from current_user
SELECT * from articles where user_id = <user_id>;
使用Ecto有相同的方法吗?
答案 0 :(得分:5)
articles = Repo.all(from a in assoc(current_user, :articles))
或将文章预加载到用户
current_user = Repo.preload(current_user, [:articles])
current_user.articles