Updating the map in changeset

I have a schema that has one field data type of map. I want to update the map using changeset.
It’s look something like this

field(settings, :map) and map has coming data like this

settings: %{“one” => 5, “two” => 15}

so what I did

 def update_setting(%User{} = user, attrs) do
    user
    |> User.update_setting_changeset(attrs)
    |> Repo.update()
 end

My changeset function looks like this


def update_setting_changeset(user, attrs) do
    user
    |> cast(attrs, [:settings])
end

how do I update the map here?

update_setting(user, %{settings: %{“one” => 10, “two” => 20}})

Thanks man.

1 Like