DEV Community

Kenzy Limon
Kenzy Limon

Posted on

Getting Started on Elixir and Ecto Part 6

Updating Data

Updating records in Ecto requires us to first get a record from the database then create a changeset from that record and pass the new changes we want to make to that record, and finally, call the Ecto.Repo.update function.

task = Taskers.Tasks |> Ecto.Query.first |> Taskers.Repo.one
changeset = Taskers.Tasks.changeset(task, %{title: "Hello Reader"})
Taskers.Repo.update(changeset)
///Just like Taskers.Repo.insert, Taskers.Repo.update will return a
///tuple and we can use the case statement to check for status
case Taskers.Repo.update(changeset) do
{:ok, task} ->
# do something with task
{:error, changeset} ->
# do something with changeset
end
view raw ecto_update.ex hosted with ❤ by GitHub

Top comments (0)

Postgres on Neon - Get the Free Plan

No credit card required. The database you love, on a serverless platform designed to help you build faster.

Get Postgres on Neon

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay