I have the following model:
schema "users" do
field :email, :string
field :subscriptions, {:array, :string}
timestamps()
end
def changeset(struct, params \\ %{}) do
struct
|> cast(params, [:email,:subscriptions])
end
I cant figure out how to update the subscriptions-array from my controller. Im looking to simply push a new string to the array:
def subscribe(conn, %{"category" => category} ) do
IO.puts category # Will print "whatever_category"
user = Repo.get(User, conn.assigns.user.id)
changeset = User.changeset(user, ?)
Repo.update(changeset)
I've tried the following:
changeset = User.changeset(user, %{subscriptions: ["test"]})
This adds "test" to the array but will get replaced if I add another value.
Thank you!
Since you've got the old value already loaded, the easiest way would be to append the value to the old list and pass that to User.changeset/2
:
User.changeset(user, %{subscriptions: user.subscriptions ++ ["test"]})
Ecto also supports the push
operation which can push an item to an array without having to load the old value in, using Repo.update
. This feature is documented here. Example:
from(u in User, where: u.id == 123) |>
Repo.update_all(push: [subscriptions: "test"])
or
from(u in User, where: u.id == 123, update: [push: [subscriptions: "test"]])) |>
Repo.update_all([])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With