The error message listed in the topic is I know posted on the web several times and I apologize that I haven’t been able to resolve in reasonable amount of time on my own - so here goes:
I am using channels to insert a new comment for a selected topic by the current user. This is so that the comment auto-updates the browser pages of other users.
The web app appears to run fine but I notices that after I added an association to the user table the auto refresh was no longer working when a new comment was added. Further investigation turned up the error message in the terminal where the Phoenix Server is running.
So I am following along a tutorial that states one way add the email field to each added comment inserted is to associate the comment changeset with the user table so I can pull the email from the user table info.
This does appear to work but as I said the auto-update is lost and there is an error message on the server.
This is the code that allowed adding the user info to the comment changeset and caused the prblem:
In the comment model file this line was changed by adding :user (a foreign key) to the derive list
@derive {Poison.Encoder, only: [:content, :user]}
In the comment_channel handler this code was added to “associate” the user info to the comment changeset
def handle_in(name, %{“content” => content}, socket) do
topic = socket.assigns.topic
user_id = socket.assigns.user_idchangeset = topic |> build_assoc(:comments, user_id: user_id) |> Comment.changeset(%{content: content}) case Repo.insert(changeset) do {:ok, comment} -> **broadcast!**(socket, "comments:#{socket.assigns.topic.id}:new", %{comment: comment} ) {:reply, :ok, socket} {:error, _reason} -> {:reply, {:error, %{errors: changeset}}, socket} end
end
You will probably need more to go on and I can provide github link or perhaps gist if needed.
Removing the extra :user from the @derive makes the error go away and the auto-update then works again but unfortunately then I have no way to extract the user email field to appear next to each comment.
My guess is that build_assoc does not play well with foreign keys or perhaps @derive is not the best way “expose” keys for relating tables.