Assoc_loaded? and nested associations


# Use a single atom to preload an association
post = post |> Repo.preload(:comments)

Great, but how about nested associations?
for instance the likes of the comments of the posts

# Use a keyword list to preload nested associations as well
posts = Repo.preload posts, [comments: [:replies, :likes], authors: [] ]

It doesn’t work for me … help!

What error do you receive?

Does the same happen when you use the query format?

# Returns all posts, their associated comments, and the associated
# likes for those comments.
from(p in Post,
  preload: [:comments, comments: :likes],
  select: p)

Unfortunately it doesn’t change anything, but I find that :

The relation is :
A has many Bees
Bee has one C
%A{ bees: [ %Bee{ c: C%{ } }, …, %Bee{ c: C%{ } } ] }

Ecto.assoc_loaded?(a.b.c) # not working
Ecto.assoc_loaded?(a.b[:c]) # is working

Without any specific errors to look at or implementation details there’s not much I can suggest.

What you refer to is the dot-access (a.b) vs. brackets access (a[:b]). The first one will raise an error if the key doesn’t exist, while the latter will simply return nil instead (Access docs).

iex(17)> Ecto.assoc_loaded?(nil)