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

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
water
I’m aware of 2 such projects, are there others? git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
itzmidinesh
Hi all, I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
peck
Hi everyone. My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
leoschweizer
Hi, we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
csmr
Hi all, I’m new here and would like to hear peoples views on my Elixir/Phoenix based game-engine! :smiley: Mutonex gamedesign was finish...
New
Stef-Verniers
Hello I’ve been coding in Elixir for almost two years now because of my current job where we implemented Elixir in our application. But ...
New

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
raa-ziq
Hi everyone :waving_hand: I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews