title: Elixir - Append to tuple in various if clauses, but then tuple keeps getting reset to blank
body: <p>This code used to work, being able to put custom messages inside <code>messages</code> depending on the various clauses and send those messages back to the front end. </p>
  
  <p>I updated Elixir. Now <code>messages</code> is always <code>{}</code> when it gets sent back to the frontend. What do I have to do to get this old code to append messages to <code>messages</code> now? It'll append them and I'll inspect right after and they will be there. But then at the end, nothing is in there. </p>
  
  <p>All my other code still works, it's just <code>messages</code> is never returning anything to the front end anymore because it becomes empty by the end of the function. It's like the variable scope inside the iff isn't the same, and its a whole different variable or something. </p>
  
  <pre><code>defmodule Api.Controllers.PutProductIsVegan do 
    import Api.UserProduct
    alias Api.UserProduct, as: UserProduct
    import Ecto.Query
    import Api.Repo
    import Plug.Conn
  
    def parse_elem("true"),  do: true
    def parse_elem("false"), do: false
  
    def put_product_is_vegan(conn) do
      messages = {}
      db_user_product = %{}
      product = Api.Product |&gt; Api.Repo.get(conn.query_params["p_id"])
      vegan_count = product.vegan_count
      not_vegan_count = product.not_vegan_count
      confirm = parse_elem(conn.body_params["confirm"])
      report = parse_elem(conn.body_params["report"])
      IO.inspect(confirm, label: "confirm")
      IO.inspect(report, label: "report")
      uid = conn.query_params["u_id"]
      pid = conn.query_params["p_id"]
      IO.inspect(uid, label: "confirm")
      IO.inspect(pid, label: "report")
  
      user_product = (from up in Api.UserProduct,
      where: up.u_id == ^uid,
      where: up.p_id == ^pid)
      |&gt; Api.Repo.one
      IO.inspect(user_product)
  
      if !user_product do
        IO.puts("insert user product")
        UserProduct.insert_user_product(conn, %{
        p_id: String.to_integer(pid), 
        u_id: uid, 
        voted_not_vegan: report,
        voted_vegan: confirm
        })
      end
  
      user_product = (from up in Api.UserProduct,
      where: up.u_id == ^uid,
      where: up.p_id == ^pid)
      |&gt; Api.Repo.one
      IO.inspect(user_product)
  
      if !user_product.voted_not_vegan &amp;&amp; report do
        IO.puts("add 1 to product.not_vegan_count")
        changeset = Api.Product.changeset(
          product, %{:not_vegan_count =&gt; not_vegan_count + 1}
        )
        case Api.Repo.update(changeset) do
          {:ok, product} -&gt; 
            messages = Tuple.append(messages, "Product updated")
          {:error, changeset} -&gt; 
            messages = Tuple.append(messages, "Product not updated")
        end
        IO.puts("set up.voted_not_vegan to true")
        changeset = Api.UserProduct.changeset(
          user_product, %{:voted_not_vegan =&gt; true}
        )
        case Api.Repo.update(changeset) do
            {:ok, product} -&gt; 
              messages = Tuple.append(messages, "Product updated")
            {:error, changeset} -&gt; 
              messages = Tuple.append(messages, "Product not updated")
        end
      end
  
      if !user_product.voted_vegan &amp;&amp; confirm do
        IO.puts("add 1 to product.vegan_count")
          changeset = Api.Product.changeset(
            product, %{:vegan_count =&gt; vegan_count + 1}
          )
          case Api.Repo.update(changeset) do
            {:ok, product} -&gt; 
              messages = Tuple.append(messages, "Product updated")
            {:error, changeset} -&gt; 
              messages = Tuple.append(messages, "Product not updated")
          end
        IO.puts("set up.voted_vegan to true") 
        IO.inspect (user_product)
        changeset = Api.UserProduct.changeset(
          user_product, %{:voted_vegan =&gt; true}
        )
  
        case Api.Repo.update(changeset) do
            {:ok, product} -&gt; 
              messages = Tuple.append(messages, "Product updated")
            {:error, changeset} -&gt; 
              messages = Tuple.append(messages, "Product not updated")
        end
      end
  
      conn
        |&gt; put_resp_content_type("application/json")
        |&gt; send_resp(200, Poison.encode!(%{
            successs: "success",
            errors: Tuple.to_list(messages)
        }))
    end
  end
  </code></pre>
  
  <p>EDIT</p>
  
  <p>From Adams answer I have done this, but the <code>messages</code> is still blank at the end:</p>
  
  <pre><code>defmodule Api.Controllers.PutProductIsVegan do 
    import Api.UserProduct
    alias Api.UserProduct, as: UserProduct
    import Ecto.Query
    import Api.Repo
    import Plug.Conn
  
    def parse_elem("true"),  do: true
    def parse_elem("false"), do: false
  
    def put_product_is_vegan(conn) do
      messages = {}
      db_user_product = %{}
      product = Api.Product |&gt; Api.Repo.get(conn.query_params["p_id"])
      vegan_count = product.vegan_count
      not_vegan_count = product.not_vegan_count
      confirm = parse_elem(conn.body_params["confirm"])
      report = parse_elem(conn.body_params["report"])
      IO.inspect(confirm, label: "confirm")
      IO.inspect(report, label: "report")
      uid = conn.query_params["u_id"]
      pid = conn.query_params["p_id"]
      IO.inspect(uid, label: "confirm")
      IO.inspect(pid, label: "report")
  
      user_product = (from up in Api.UserProduct,
      where: up.u_id == ^uid,
      where: up.p_id == ^pid)
      |&gt; Api.Repo.one
      IO.inspect(user_product)
  
      if !user_product do
        IO.puts("insert user product")
        UserProduct.insert_user_product(conn, %{
        p_id: String.to_integer(pid), 
        u_id: uid, 
        voted_not_vegan: report,
        voted_vegan: confirm
        })
      end
  
      user_product = (from up in Api.UserProduct,
      where: up.u_id == ^uid,
      where: up.p_id == ^pid)
      |&gt; Api.Repo.one
      IO.inspect(user_product)
  
      if user_product.voted_not_vegan &amp;&amp; report do
        messages = Tuple.append(messages, "You have already reported this product")
      end
  
      if !user_product.voted_not_vegan &amp;&amp; report do
        IO.puts("add 1 to product.not_vegan_count")
        changeset = Api.Product.changeset(
          product, %{:not_vegan_count =&gt; not_vegan_count + 1}
        )
        messages =
        case Api.Repo.update(changeset) do
          {:ok, product} -&gt; Tuple.append(messages, "Product updated")
          {:error, changeset} -&gt; Tuple.append(messages, "Product not updated")
        end
        IO.puts("set up.voted_not_vegan to true")
        changeset = Api.UserProduct.changeset(
          user_product, %{:voted_not_vegan =&gt; true}
        )
        messages =
        case Api.Repo.update(changeset) do
            {:ok, product} -&gt; Tuple.append(messages, "Product updated")
            {:error, changeset} -&gt; Tuple.append(messages, "Product not updated")
        end
      end
  
      if !user_product.voted_vegan &amp;&amp; confirm do
        IO.puts("add 1 to product.vegan_count")
          changeset = Api.Product.changeset(
            product, %{:vegan_count =&gt; vegan_count + 1}
          )
          messages =
          case Api.Repo.update(changeset) do
            {:ok, product} -&gt; Tuple.append(messages, "Product updated")
            {:error, changeset} -&gt; Tuple.append(messages, "Product not updated")
          end
        IO.puts("set up.voted_vegan to true") 
        IO.inspect (user_product)
        changeset = Api.UserProduct.changeset(
          user_product, %{:voted_vegan =&gt; true}
        )
  
        messages =
        case Api.Repo.update(changeset) do
            {:ok, product} -&gt; Tuple.append(messages, "Product updated")
            {:error, changeset} -&gt; Tuple.append(messages, "Product not updated")
        end
      end
  
      IO.inspect(messages) # this is {}
  
      conn
        |&gt; put_resp_content_type("application/json")
        |&gt; send_resp(200, Poison.encode!(%{
            successs: "success",
            errors: Tuple.to_list(messages)
        }))
    end
  end
  </code></pre>