I was already using Ecto.Changeset and it was still failing. The problem was that I was not using the action on_replace: :nilify in the model. I managed to find the fix by accident since the documentation of put_assoc/4 doesn't specify anything about setting on_replace for the relation. I also had to preload the category's :products association before turning it into a changeset. I believe it would be nice if this would be also be mentioned in the docs.
From what I've read I have to always pull out the existing products for a category in order to add a new product to that specific category.
%{product | category_id: category.id}
add_one_product(category_without_preloading_products, product)
Does the Repo.update method update all the products or does it do a diff between the existing products of the category and the list passed to the put_assoc method?
p = Product |> Repo.get_by(id: 107)
ch = Ecto.Changeset.change(p, category_id: 204)
Repo.update!(ch)
--
You received this message because you are subscribed to the Google Groups "elixir-ecto" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-ecto+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-ecto/4fa18fd7-7046-4de6-bfdf-4e5a459f88f3%40googlegroups.com.
--
You received this message because you are subscribed to the Google Groups "elixir-ecto" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-ecto+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-ecto/ffddd7f3-d565-4204-802a-776e1ae5ac19%40googlegroups.com.