title: Encoding a Ecto Model to JSON in elixir
body: <p>I am going over the following tutorial in an attempt to get my head around elixir and phoenix:</p>
  
  <p><a href="https://thoughtbot.com/blog/testing-a-phoenix-elixir-json-api" rel="noreferrer">https://thoughtbot.com/blog/testing-a-phoenix-elixir-json-api</a></p>
  
  <p>I am running into an issue with the test, mainly using Poison.encode! on the Contact model. I get the following error:</p>
  
  <pre><code>unable to encode value: {nil, "contacts"}
  </code></pre>
  
  <p>This led me to the following issue:</p>
  
  <p><a href="https://github.com/elixir-lang/ecto/issues/840" rel="noreferrer">https://github.com/elixir-lang/ecto/issues/840</a>
  and the fix:
  <a href="https://coderwall.com/p/fhsehq/fix-encoding-issue-with-ecto-and-poison" rel="noreferrer">https://coderwall.com/p/fhsehq/fix-encoding-issue-with-ecto-and-poison</a></p>
  
  <p>I have added the code from the blog article into lib/poison_encoder.ex, but I now get the following error:</p>
  
  <pre><code>no function clause matching in Poison.Encoder.Any.encode/2
  </code></pre>
  
  <p>The code I have in lib/poison_encoder.ex:</p>
  
  <pre><code>defimpl Poison.Encoder, for: Any do
    def encode(%{__struct__: _} = struct, options) do
      map = struct
            |&gt; Map.from_struct
            |&gt; sanitize_map
      Poison.Encoder.Map.encode(map, options)
    end
  
    defp sanitize_map(map) do
      Map.drop(map, [:__meta__, :__struct__])
    end
  end
  </code></pre>