Selectively force loading associations in Ecto.Repo.preload()


Small question about Ecto.Repo.preload(). I was trying to do something like this:

    updated_post =
      |> Repo.update!()
      |> Repo.preload(
        comments: from(c in Comments, where: is_nil(c.some_field)), force: true

But I get “schema … does not have association :force” error.

Seems like I can only either do:

        comments: from(c in Comments, where: is_nil(c.some_field))


        :comments, force: true

If I use the first option I have some missing comment associations but the second option contains associations that I don’t want to have included.

Would it be possible somehow to combine those two into one preload statement (from(…) and force flag)?

When using both parameters you need to add the square brackets for the non-last parameter.

1 Like

Thanks a lot! That worked.