Hi,
What is the idiomatic way to update a record? I need to handle the case where the id may not be found, and return the error if that is the case. I’ve come up with the following code, although it looks rather ugly:
def update(params, person_id) do
person = Repo.get(Person, person_id)
person =
person &&
person
|> cast(params, @permitted)
|> validate()
|> Repo.update()
case person do
nil ->
{:error,
%Person{}
|> change()
|> add_error(:person_id, "not found")}
{:ok, p} -> {:ok, p}
err -> err
end
end
Thank you!