Skip to content

Instantly share code, notes, and snippets.

@moxley
Created March 31, 2016 23:32
Show Gist options
  • Save moxley/ef318feec9652e8ce9eef1150dafbb49 to your computer and use it in GitHub Desktop.
Save moxley/ef318feec9652e8ce9eef1150dafbb49 to your computer and use it in GitHub Desktop.
Strip an Ecto record of attributes that cannot be encoded to JSON
record = Enum.reduce(Map.keys(record), %{}, fn (key, map) ->
value = Map.get(record, key)
key_to_add = cond do
is_map(value) && Map.get(value, :__struct__, nil) == Elixir.Ecto.Association.NotLoaded ->
nil
key == :__meta__ ->
nil
key == :__struct__ ->
nil
true ->
key
end
if key_to_add do
map |> Map.merge(%{key_to_add => value})
else
map
end
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment