Skip to content

Commit

Permalink
Update put_assoc cheatsheet to use changesets (#4437)
Browse files Browse the repository at this point in the history
The existing docs use update_in on a struct and pass that to put_assoc
which doesn't actually perform any updates because:

"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."
  • Loading branch information
patrickdavey committed Jun 20, 2024
1 parent 5e9476f commit b64cdcf
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions guides/cheatsheets/associations.cheatmd
Original file line number Diff line number Diff line change
Expand Up @@ -268,12 +268,12 @@ movie =
|> Repo.preload(:characters)

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

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

{:ok, movie} =
Expand Down

0 comments on commit b64cdcf

Please sign in to comment.