Problem when trying to fetch results from database in a channel

I’m getting the following error when trying to make an insert in the database through a channel:

ERROR

[error] GenServer #PID<0.418.0> terminating
** (KeyError) key :user_id not found in: %{product: %Stock.Product{__meta__: #Ecto.Schema.Metadata<:loaded, "products">, comments: [], description: "Proteus Core Version", id: 11, name: "Mouse Logitech 
g502", quantity: 50, user: #Ecto.Association.NotLoaded<association :user is not loaded>, user_id: 1, 
value: 400.0}}
    (stock) web/channels/comments_channel.ex:19: Stock.CommentsChannel.handle_in/3
    (phoenix) lib/phoenix/channel/server.ex:226: anonymous fn/4 in Phoenix.Channel.Server.handle_info/2
    (stdlib) gen_server.erl:616: :gen_server.try_dispatch/4
    (stdlib) gen_server.erl:686: :gen_server.handle_msg/6
    (stdlib) proc_lib.erl:247: :proc_lib.init_p_do_apply/3
Last message: %Phoenix.Socket.Message{event: "comment:add", payload: %{"content" => "Test"}, ref: "2", topic: "comments:11"}
State: %Phoenix.Socket{assigns: %{product: %Stock.Product{__meta__: #Ecto.Schema.Metadata<:loaded, "products">, comments: [], description: "Proteus Core Version", id: 11, name: "Mouse Logitech g502", quantity: 50, user: #Ecto.Association.NotLoaded<association :user is not loaded>, user_id: 1, value: 400.0}}, channel: Stock.CommentsChannel, channel_pid: #PID<0.418.0>, endpoint: Stock.Endpoint, handler: Stock.UserSocket, id: nil, joined: true, pubsub_server: Stock.PubSub, ref: nil, serializer: Phoenix.Transports.WebSocketSerializer, topic: "comments:11", transport: Phoenix.Transports.WebSocket, transport_name: :websocket, transport_pid: #PID<0.416.0>}
[info] JOIN comments:11 to Stock.CommentsChannel
  Transport:  Phoenix.Transports.WebSocket
  Parameters: %{}
[debug] QUERY OK source="products" db=0.0ms
SELECT p0."id", p0."name", p0."description", p0."value", p0."quantity", p0."user_id" FROM "products" 
AS p0 WHERE (p0."id" = $1) [11]
[debug] QUERY OK source="comments" db=15.0ms
SELECT c0."id", c0."content", c0."user_id", c0."product_id", c0."inserted_at", c0."updated_at", c0."product_id" FROM "comments" AS c0 WHERE (c0."product_id" = $1) ORDER BY c0."product_id" [11]
[info] Replied comments:11 :ok

** My Channel File **

defmodule Stock.CommentsChannel do
  use Stock.Web, :channel
  alias Stock.{Product, Comment}

  def join("comments:" <> product_id, _params, socket) do
    product_id = String.to_integer(product_id)
    product = Product
      |> Repo.get(product_id)
      |> Repo.preload(:comments)

    {:ok, %{comments: product.comments}, assign(socket, :product, product)}
  end

  def handle_in(name, %{"content" => content}, socket) do
    product = socket.assigns.product
    # user_id = socket.assigns.user_id

    changeset = product
      |> build_assoc(:comments, user_id: socket.assigns.user_id)
      |> Comment.changeset(%{content: content})

    case Repo.insert(changeset) do
      {:ok, comment} ->
        broadcast!(socket, "comments:#{socket.assigns.product.id}:new",
          %{comment: comment}
        )
        {:reply, :ok, socket}
      {:error, _reason} ->
        {:reply, {:error, %{errors: changeset}}, socket}
    end
  end
end

This is the line the error is coming from. You aren’t assigning the user id to the socket assigns.