patrickdavey
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.
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 6 to 1- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
patrickdavey
Update put_assoc cheatsheet to use changesets by patrickdavey · Pull Request #4437 · elixir-ecto/ecto · GitHub All merged in now
LostKobrakai
Docs can be incorrect as well – in this case the cheatsheet seems incorrect. You can report such issues on the ecto repo.
dimitarvp
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?
patrickdavey
The first doc I linked to is titled
updating-all-associated-records-using-internal-data→ Associations — Ecto v3.14.0 and has this example:In that example (which I was trying to follow) it really looks like they’re using a schema and getting back the
charactersand then usingupdate_into change the map and then passing it toRepo.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.dimitarvp
I’m not sure I understand what your problem with the docs is? They’re clearly stating a caveat.
patrickdavey
according to Ecto.Changeset — Ecto v3.14.0 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?