patrickdavey
Trouble updating an association - changes not persisted
I’m learning Ecto, and having issues trying to update following the docs. In my repo User has_many UserOrganisations, and there’s boolean called home on UserOrganisations. I’m attempting to set them all to false.
First: I load my user.
user = Repo.get!(User, 1) |> Repo.preload(:user_organisations)
Looking at the docs it really looks like I should be able to do:
user_orgs = Enum.map(user.user_organisations, &(put_in(&1.home, false)))
(this correctly returns the updated structs with home set to false)
However, performing the following makes no changes.
user |> change() |> put_assoc(:user_organisations, user_orgs) |> Repo.update()
If instead I create the orgs by explicitly wrapping them in a changeset
user_orgs = Enum.map(user.user_organisations, &(change(&1, %{home: false})))
Then the changes are made.
I’m assuming I’m misunderstanding something here, but, on the off-chance that the docs are wrong I thought I’d double check. I also tried using update_in like in the docs, but, that didn’t work either.
Most Liked
LostKobrakai
Docs can be incorrect as well – in this case the cheatsheet seems incorrect. You can report such issues on the ecto repo.







