EddTally

EddTally

Updating a Nested Map in a Schema

Hi,
I am currently having problems trying to update a nested map in a Schema since it doesn’t allow for Access behaviour I can’t just Kernel.put_in etc…

My Schema is as follows:

> %PhoenixApi.Schema.Test{
>   __meta__: #Ecto.Schema.Metadata<:loaded, "test_table">,
>   id: 1,
>   delete: false,
>   person: %{
>     "city" => %{"55" => "London"},
>     "name" => "john cena"
>     "address" => "55 Test Drive"
>   }
> }

I use get_test(id) to retrieve this information, but I would like to change the persons name and city before updating it/sending it back into my table.
This person is stored as a :map in the schema.
I know update needs a changeset to work, it’s how to make the changes in the changeset for Ecto.update and updating nested maps in structs is confusing me, sorry.
Thank you for reading.

Marked As Solved

EddTally

EddTally

I have created a workaround? method?

I pattern match out of the get_test(id).

old_person = get_test(id)
%{id: id, delete: delete, person: person} = old_person
new_person = %{id: id, delete: delete, person: person}

Then I can use:
new_person = Kernel.put_in(new_person, [:person, "name"], "New Name")
because it is purely just a map now and not a Schema, then update row via the auto generated context function update_person:

> def update_person(%Test{} = test, attrs) do
>     test
>     |> Test.changeset(attrs)
> 	|> Repo.update()
>   end

by

update_person(old_person, new_person)

and BAM it’s updated.
If anyone has any improvements I would love to hear them!

Also Liked

vfsoraki

vfsoraki

You may also look into embeds_one to have your map treated as an embedded schema, with defined fields/changeset.

As for what you’ve achieved, that what I would do for a bare map too.

Where Next?

Popular in Questions Top

JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
Fl4m3Ph03n1x
Background Let’s assume I have a typical GenServer that receives messages as requests, does some operation in a DB and returns responses....
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
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
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
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
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New

We're in Beta

About us Mission Statement