script

script

I have this struct of preload records.
I want to remove
__meta__ and Ecto.Association.NotLoaded from the main struct as well as from the nested array of preloaded records. To make it a simple map

  %Facility{
  __meta__: #Ecto.Schema.Metadata<:loaded, "facilities">,
  id: 4875,
  instance: #Ecto.Association.NotLoaded<association :instance is not loaded>,
  name: "Facility",
 units: [
%Unit{
  __meta__: #Ecto.Schema.Metadata<:loaded, "units">,
  facility: #Ecto.Association.NotLoaded<association :facility is not loaded>,
  facility_id: 4875,
  id: 1593,
  name: "Storage",
  rooms: #Ecto.Association.NotLoaded<association :rooms is not loaded>
},
%Unit{
  __meta__: #Ecto.Schema.Metadata<:loaded, "units">,
  cdc_location_class_id: nil,
  facility: #Ecto.Association.NotLoaded<association :facility is not loaded>,
  facility_id: 4875,
  id: 1594,
  name: "Storage unit",
  rooms: #Ecto.Association.NotLoaded<association :rooms is not loaded>
}
],
users: #Ecto.Association.NotLoaded<association :users is not loaded>
}

Is it possible?

Thanks

Showing Posts 1 to 10

idi527

idi527

Is it possible?

Yes, try writing a recursive function.

kokolegorille

kokolegorille

Everything is about transforming data… Another way is to use Enum and Map modules :slight_smile:

iex> h Enum.
EmptyError           OutOfBoundsError     all?/1               
all?/2               any?/1               any?/2               
at/2                 at/3                 chunk_by/2           
chunk_every/2        chunk_every/3        chunk_every/4        
chunk_while/4        concat/1             concat/2             
count/1              count/2              dedup/1              
dedup_by/2           drop/2               drop_every/2         
drop_while/2         each/2               empty?/1             
fetch!/2             fetch/2              filter/2             
find/2               find/3               find_index/2         
find_value/2         find_value/3         flat_map/2           
flat_map_reduce/3    group_by/2           group_by/3           
intersperse/2        into/2               into/3               
join/1               join/2               map/2                
map_every/3          map_join/2           map_join/3           
map_reduce/3         max/1                max/2                
max_by/2             max_by/3             member?/2            
min/1                min/2                min_by/2             
min_by/3             min_max/1            min_max/2            
min_max_by/2         min_max_by/3         random/1             
reduce/2             reduce/3             reduce_while/3       
reject/2             reverse/1            reverse/2            
reverse_slice/3      scan/2               scan/3               
shuffle/1            slice/2              slice/3              
sort/1               sort/2               sort_by/2            
sort_by/3            split/2              split_while/2        
split_with/2         sum/1                take/2               
take_every/2         take_random/2        take_while/2         
to_list/1            uniq/1               uniq_by/2            
unzip/1              with_index/1         with_index/2         
zip/1                zip/2                

And Map

iex> h Map.       
delete/2             drop/2               equal?/2             
fetch!/2             fetch/2              from_struct/1        
get/2                get/3                get_and_update!/3    
get_and_update/3     get_lazy/3           has_key?/2           
keys/1               merge/2              merge/3              
new/0                new/1                new/2                
pop/2                pop/3                pop_lazy/3           
put/3                put_new/3            put_new_lazy/3       
replace!/3           split/2              take/2               
to_list/1            update!/3            update/4             
values/1
script

script OP

hey thanks for your input. I have one question is it also possible to add list of maps to a map in a specific order:

Like these two:

                    params = %{name: "john wick"}
                    list_of_maps = [%{id: 1}, %{id: 2}, %{id: 3}]

            result = %{name: "john wick", some_key: list_of_maps}

When i try to add them the order is reversed. I know its off topic just curious :slightly_smiling_face:

Can you suggest alternate appraoch?
Thanks

idi527

idi527

Can you suggest alternate appraoch?

Add them in the reversed order? I’m not sure what you are doing, though. Where do the maps come from?

script

script OP

If i added them in the reverse order then it should be a list. i want it to be a map. Params come from the function. And I want it to manipulate it and add list of items in it like the example above…

NobbZ

NobbZ

This should not reverse anything, could you please be more specific about the problem?

script

script OP

Params
|> Map.put(:key, list_of_maps)
The output is

      %{ key: [%{id:1}, ....], name: "john wick"}

What I want is this:

      %{ name: "john wick", key: [%{id:1}, ....] }
idi527

idi527

These outputs are identical. Try comparing them with === if you are in doubt.

NobbZ

NobbZ

There is no difference in both, those are equivalent from elixirs point of view.

Keys in a map are without any ordering guarantees.

script

script OP

Thanks

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews