Board field updates incorrectly: replaces full 9-cell state with single value

In My Phoenix (Elixir) App I have a board field in a table as an array of strings ({:array, :string}) with a default value of ["", "", "", "", "", "", "", "", ""].

However, when I update using ecto only one cell in the 9-element array, the entire board field in the database is being replaced with a single-element array — for example:

{"X"}

instead of the expected:

{"X", "", "", "", "", "", "", "", ""}
current_board = game.board || List.duplicate(“”, 9)
new_board = List.replace_at(current_board, position, player_symbol)

board in migration file
add :board, {:array, :string}, default: [“”, “”, “”, “”, “”, “”, “”, “”, “”]

board in schema file
field :board, {:array, :string}, default: List.duplicate(“”, 9)

need help please

The replace_at/3 function will do what you expect, here.

iex> List.duplicate("", 3) |> List.replace_at(1, "X")
["", "X", ""]

The problem is likely elsewhere, but there is no way for us to know. What have you tried so far?

1 Like