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 |
Top comments (0)