Skip to content

Commit

Permalink
Add examples to preload (#4274)
Browse files Browse the repository at this point in the history
  • Loading branch information
dkuku authored Sep 1, 2023
1 parent a2e5b82 commit c53d13f
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/ecto/query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2543,7 +2543,15 @@ defmodule Ecto.Query do
Repo.all from a in Author, preload: [posts: ^{posts_query, [:comments]}]
If you prefer, you can also add additional preloads directly in the
`posts_query`.
`posts_query`:
posts_query =
from p in Post, where: p.state == :published, preload: :related_posts
The same can be written as pipe based query:
posts_query =
Post |> where([p], p.state == :published) |> preload(:related_posts)
Note: keep in mind operations like limit and offset in the preload
query will affect the whole result set and not each association. For
Expand Down

0 comments on commit c53d13f

Please sign in to comment.