Hey all,
On a schema Sale
, I have many_to_many :products, Product, join_through: SaleProduct, on_replace: :delete
. I also have a Changeset for Sale
, like
@doc """
Updates and reorders products associated with a sale
"""
def products_changeset(%Sale{} = sale, attrs) do
sale
|> cast(attrs, [])
|> put_assoc(:products, parse_and_get_products(attrs))
end
defp parse_and_get_products(%{"product_ids"=> product_ids}) do
Repo.all(from p in Product, where: p.id in ^product_ids)
end
given put_assoc(:products, [])
that should delete and join rows for SaleProduct
yes?
I’ve used this exact same setup elsewhere in my application and it works as expected, and I’ve checked over my schemas, etc and I can’t figure out why this one doesn’t work as I expect. What seems to be happening is when I pass a list of found products, it always inserts them all, and doesn’t ever remove the existing relations that aren’t present in the list passed.