From fbd7e4cb19a044571afc61409e98154d2ea03d0c Mon Sep 17 00:00:00 2001 From: Juanmi Acosta Date: Sun, 15 Feb 2026 19:55:52 +0100 Subject: [PATCH 1/3] docs: clarify index shifting in cast_assoc/3 when using drop_param --- lib/ecto/changeset.ex | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/lib/ecto/changeset.ex b/lib/ecto/changeset.ex index 1c4853ba0f..bdc18327ed 100644 --- a/lib/ecto/changeset.ex +++ b/lib/ecto/changeset.ex @@ -1160,6 +1160,24 @@ defmodule Ecto.Changeset do changeset |> cast_assoc(:children, sort_param: ..., with: &child_changeset/3) + When using `:drop_param` without `:sort_param`, the remaining elements are + re-indexed sequentially starting from 0. For example, if you have the following + parameters: + + %{ + "addresses" => %{ + 0 => %{"street" => "First St"}, + 1 => %{"street" => "Second St"}, + 2 => %{"street" => "Third St"} + }, + "addresses_drop" => [1] + } + + The element originally at index `2` ("Third St") will be passed to the `:with` + function with position `1`, because the list is compacted after the drop + operation. If you need to preserve the original semantic positions, ensure + you also provide a `:sort_param`. + These parameters can be powerful in certain UIs as it allows you to decouple the sorting and replacement of the data from its representation. From 1268a5a1a7dc94238631d60d91b375c284ecbdb6 Mon Sep 17 00:00:00 2001 From: Juanmi Acosta Date: Sun, 15 Feb 2026 22:52:11 +0100 Subject: [PATCH 2/3] fix : remove unnecessary sort_param reference from documentation --- lib/ecto/changeset.ex | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/ecto/changeset.ex b/lib/ecto/changeset.ex index bdc18327ed..f716a0c313 100644 --- a/lib/ecto/changeset.ex +++ b/lib/ecto/changeset.ex @@ -1175,8 +1175,7 @@ defmodule Ecto.Changeset do The element originally at index `2` ("Third St") will be passed to the `:with` function with position `1`, because the list is compacted after the drop - operation. If you need to preserve the original semantic positions, ensure - you also provide a `:sort_param`. + operation. These parameters can be powerful in certain UIs as it allows you to decouple the sorting and replacement of the data from its representation. From 1f7b1c2fdbfaff8de600af0a2b1a8f7a0d8bb4e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Mon, 16 Feb 2026 09:36:06 +0100 Subject: [PATCH 3/3] Apply suggestions from code review --- lib/ecto/changeset.ex | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/ecto/changeset.ex b/lib/ecto/changeset.ex index f716a0c313..a132467403 100644 --- a/lib/ecto/changeset.ex +++ b/lib/ecto/changeset.ex @@ -1160,9 +1160,8 @@ defmodule Ecto.Changeset do changeset |> cast_assoc(:children, sort_param: ..., with: &child_changeset/3) - When using `:drop_param` without `:sort_param`, the remaining elements are - re-indexed sequentially starting from 0. For example, if you have the following - parameters: + When using `:drop_param`, the remaining elements are re-indexed sequentially + starting from 0. For example, if you have the following parameters: %{ "addresses" => %{