Sebb

Sebb

Get a map instead of a list from embeds_many

I’m building an Ecto schema to cast a JSON into a struct.

The data looks like this:

%{
  name: "Bob", 
  ...
  body_parts: [
    %{id: "head", count: 1},
    %{id: "hands", count: 2},
  ]
}

I’d like to have the body parts in a map (indexed by their ID) like:

%Person{
  body_parts: %{
    "head" => %BodyPart{count: 1, id: "head"},
    "hands" => %BodyPart{count: 2, id: "hands"}
  },
  ...
  name: "Bob"
}

but embeds_many(:body_parts, BodyPart) (obviously) gives me a list.
Is there a clean way to do that? I could just post-process the Person but my schema would be lying then.
If that helps I could change the input to a map.

defmodule BodyPart do
  use Ecto.Schema

  @primary_key false
  embedded_schema do
    field(:id, :string)
    field(:count, :integer)
  end

  def changeset(body_part, params) do
    Ecto.Changeset.cast(body_part, params, [:id, :count])
  end
end

defmodule Person do
  use Ecto.Schema

  @primary_key false
  embedded_schema do
    field(:id, :integer)
    ...
    embeds_many(:body_parts, BodyPart)
  end

  def changeset(params) do
    %Person{}
    |> Ecto.Changeset.cast(params, [:id, :name, :age, :hobbies, :friends])
    |> Ecto.Changeset.cast_embed(:body_parts)
  end
end

First Post!

dimitarvp

dimitarvp

This got me curious: why do you need this connection as a map? Why is the normal embedding not good enough?

Where Next?

Popular in Questions Top

gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

Other popular topics Top

lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New

We're in Beta

About us Mission Statement