Skip to content

Commit 3be4e60

Browse files
authored
Clarify index shifting in cast_assoc/3 when using drop_param (#4705)
1 parent 7504d0f commit 3be4e60

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

lib/ecto/changeset.ex

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,6 +1160,22 @@ defmodule Ecto.Changeset do
11601160
changeset
11611161
|> cast_assoc(:children, sort_param: ..., with: &child_changeset/3)
11621162
1163+
When using `:drop_param`, the remaining elements are re-indexed sequentially
1164+
starting from 0. For example, if you have the following parameters:
1165+
1166+
%{
1167+
"addresses" => %{
1168+
0 => %{"street" => "First St"},
1169+
1 => %{"street" => "Second St"},
1170+
2 => %{"street" => "Third St"}
1171+
},
1172+
"addresses_drop" => [1]
1173+
}
1174+
1175+
The element originally at index `2` ("Third St") will be passed to the `:with`
1176+
function with position `1`, because the list is compacted after the drop
1177+
operation.
1178+
11631179
These parameters can be powerful in certain UIs as it allows you to decouple
11641180
the sorting and replacement of the data from its representation.
11651181

0 commit comments

Comments
 (0)