patrickdavey

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.

Showing Posts 6 to 1

LostKobrakai

LostKobrakai

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

dimitarvp

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? :smiley:

patrickdavey

patrickdavey OP

The first doc I linked to is titled updating-all-associated-records-using-internal-dataAssociations — Ecto v3.14.0 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.

dimitarvp

dimitarvp

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

patrickdavey

patrickdavey OP

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?

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews