Search code examples
elixirphoenix-frameworkecto

How to perform ActiveRecord's query current_user.articles equivalent with Ecto?


In Rails AR is possible to run queries like this:

current_user.articles # fetch all articles from current_user
SELECT * from articles where user_id = <user_id>;

There's an equivalent way to do this with Ecto?


Solution

  • articles = Repo.all(from a in assoc(current_user, :articles))
    

    or preload the articles into the user

    current_user = Repo.preload(current_user, [:articles])
    current_user.articles