chemist

chemist

Ecto cast_assoc - %UndefinedFunctionError

Will love some advice

Basically i have a map comprising of another nested map.

I am using Ecto.Changeset to cast the values and validate them

    input: 
   { document: {"id": 2091}, players: ["1", "2"], details: {"code": "100", "id: "saas"}}

    data = %{}
    types = %{document: :map, players: {:array, :string}, details: :assoc}
    details_types = %{code: :string, id: :string}

    changeset =
    {data, types}
    |> Changeset.cast(input, Map.keys(types))
    |> Changeset.cast_assoc(:details, Map.keys(details_types))

But doing this generates an error:

%UndefinedFunctionError{arity: 1, function: :cast, message: nil, module: :assoc, reason: nil}, stack: [{:assoc, :cast, [%{"code" => "100", "id" => "saas"}]

Many thanks

Most Liked

trisolaran

trisolaran

yes because you’re using cast_assoc, which you can only use if you define an association, but you haven’t defined any. You define an association by declaring a schema and calling has_one, has_many, embeds_one, embeds_many etc.

You should rewrite your code using a schema definition (it doesn’t appear like you’re using any, as you’re working with inline types), and/or explain a bit better what you’re trying to achieve.

I also recommend reading the ecto schema docs.

trisolaran

trisolaran

The basic problem here is that you’re defining details to be of type :assoc but :assoc is not a type, unless you defined it somewhere else. Ecto is telling you that it was trying to cast details but it could not find any method called cast in the assoc module. Here’s the list of Ecto types available by default.

If :details is an arbitrary map, then you should use type :map for it. If it has to follow a predefined structure, then I would define a schema for it and turn it into an association. In general, if you’re dealing with complex or nested data structures, it’s easier to work with schemas rather than using inline {data, types} tuples.

chemist

chemist

Hi thanks very much. Apologies but if i assign a type :map to details, i get an error that an :assoc is expected which is why i defined the type as an :assoc.

Where Next?

Popular in Questions Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
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
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
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
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
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
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44139 214
New

We're in Beta

About us Mission Statement