How to execute ActiveRecord query current_user.articles is equivalent to Ecto?

In Rails AR, you can run queries as follows:

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

Is there an equivalent way to do this with Ecto?

+4
source share
1 answer
articles = Repo.all(from a in assoc(current_user, :articles))

or preload articles in the user

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

Source: https://habr.com/ru/post/1612848/


All Articles