Ecto nested errors, how to traverse and translate

250 views
Skip to first unread message

sendme...@gmail.com

unread,
Feb 22, 2016, 5:53:43 AM2/22/16
to phoenix-talk
For example, we have Faculties and Specialities. Each Faculty has many Specialities.

If we insert Faculties and Specialities using Ecto 2.0 and have nested errors (in Specialities), how can we traverse and return translated errors?

My current code was:

changeset = Faculty.changeset(%Faculty{}, attrs["data"], :specialities, [])

case Repo.insert(changeset) do
{:ok, _model} -> {:reply, :ok, socket}
{:error, changeset} ->
{:reply, {:error, %{errors: translate_errors(changeset)}}, socket}
end
defp translate_errors(changeset) do
Ecto.Changeset.traverse_errors(changeset, &translate_error/1)
end

Chris McCord

unread,
Feb 23, 2016, 8:12:50 AM2/23/16
to phoeni...@googlegroups.com
traverse_errors will also traverse the nested errors from the relationships. Are you seeing differently?

--
You received this message because you are subscribed to the Google Groups "phoenix-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to phoenix-talk...@googlegroups.com.
To post to this group, send email to phoeni...@googlegroups.com.
Visit this group at https://groups.google.com/group/phoenix-talk.
To view this discussion on the web visit https://groups.google.com/d/msgid/phoenix-talk/0752d39e-9958-4110-8c2c-aca01e243dd0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

s3xual...@gmail.com

unread,
Feb 23, 2016, 8:20:32 AM2/23/16
to phoenix-talk
It was strange, but it didn't work. I tried multiple times, restarted the server - still no luck.

Jose suggested to create a new repo and test there. It worked in the new project.
So, I tried to deps clean & deps get and it started to work in my current project.
The code is the same, only reinstalled the deps and it worked. Still don't know, what was the error.

Thank you, everything is working great now! 
Reply all
Reply to author
Forward
0 new messages