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.

according to Ecto.Changeset — Ecto v3.11.2 it says (when you’re passing structs)

Different to passing changesets, structs are not change tracked in any fashion. In other words, if you change a comment struct and give it to put_assoc/4, the updates in the struct won't be persisted. You must use changesets instead.

So, I guess that’s why my first approach is falling over, but, isn’t that what the docs are doing?

I’m not sure I understand what your problem with the docs is? They’re clearly stating a caveat.

The first doc I linked to is titled updating-all-associated-records-using-internal-dataAssociations — Ecto v3.11.2 and has this example:

movie =
  Repo.get_by!(Movie, title: "The Shawshank Redemption")
  |> Repo.preload(:characters)

IO.inspect(movie.characters)
#=> [%{name: "Andy Dufresne", age: 50},
#=>  %{name: "Red", age: 60}]

characters =
  Enum.map(characters, fn character ->
    update_in(character.age, &(&1 + 1)))
  end)

{:ok, movie} =
  movie
  |> change()
  |> put_assoc(:characters, characters)
  |> Repo.update()

movie.characters |> Enum.map(&(&1.age)) |> IO.inspect
#=> [51, 61]

In that example (which I was trying to follow) it really looks like they’re using a schema and getting back the characters and then using update_in to change the map and then passing it to Repo.update(). Nowhere on that page do they say “structs are not change tracked in any fashion”, and, I guess I just don’t see how the example on that page is supposed to work at all :). Probably I’m just missing something - I’m very new to all this!. Thanks for the response.

Ah, I see.

Also, your last “code” line is your comment and it’s really long and hard to read. Take it out of the coding block? :smiley:

Docs can be incorrect as well – in this case the cheatsheet seems incorrect. You can report such issues on the ecto repo.

1 Like

Update put_assoc cheatsheet to use changesets by patrickdavey · Pull Request #4437 · elixir-ecto/ecto · GitHub All merged in now :slight_smile: