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

Hey!

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

    updated_post =
      changeset
      |> 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:

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

or

 Repo.preload(
        :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.