Ecto.put_assoc confusion/sanity check

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.

I’ve had a similar problem in an umbrella project I’m working on. It would append the assoc in the join table instead of deleting.

I rebuilt (rm -r _build in root project folder) and it started deleting them on replace.

I gave that a quick try but no dice :frowning: I’ll dig into it again in a bit and let you know if I can figure anything out.