cevado

cevado

Proposal: Allow nested schemaless changeset with Ecto

Being able to build nested relations in a schemaless changeset would be helpful to deal with complex forms on Phoenix without the need to create either an intermediary embed schema to deal with complex forms or constraint the form to express only the db ecto schema.
Also it allows to use the cast/validation tools of ecto with complex data without the need to create a embed schema.

I think this could be achieved with cast_assoc/3 work with schemaless changeset for example:

defmodule Test do
  import Ecto.Changeset
  def nested(params) do
    {%{field1: %{a: 1}}, %{field1: {:map, :a}, field2: {:list, :b}}
    |> cast(params, [])
    |> cast_assoc(:field1, with: &function/2)
    |> cast_assoc(:field2, with: &function/2)
  end
  
  defp function({:a, data}, params) do
    {data, %{a: :integer}}
    |> cast(params, [:a])
  end
  
  defp function({:b, data}, params) do
    {data, %{b: :string}}
    |> cast(params, [:b])
  end
end

iex> Test.nested(%{field1: %{a: 5, b: 2}, field2: [%{a: 1, b: "thing"}]})
%{field1: %{a: 5}, field2: [%{b: "thing"}]}

Constraints to casting assoc on a schemaless changeset: it always have to include a with: option.
In this way put_assoc/4 would also require the with: option and have the same behavior of overriding whatever value that was present in original params of the parent assoc.

Where Next?

Popular in Proposals: Ideas Top

maxpohlmann
In our application, we have many structs that contain lists of floats and, especially in test, we often use pattern matching on these obj...
New
Asd
Process.resolve/1 What it does Takes any possible name of the process and returns a pid of the process or port (or nil if none found). Si...
New
dimitarvp
To @jonatanklosko and @the-mikedavis: I see that there is a Rust crate at crates.io: Rust Package Registry but it is pointing at https:/...
New
mudasobwa
I am not sure it deserves to be discussed in the mailing list, so I’d start here. assert/1 and/or refute/1 macros print the following er...
New
markevans
Hi! I’m excited about everything that’s going on re. gradual typing and am really pleased to see that Jose and the team seem to be think...
New
dli
Ecto currently supports some data-modifying WITH statements / CTEs for Postgres: Options: […] :operation - one of :all , :update_all ,...
New
7rans
I implemented Access behavior for a struct today. Pseudo-code… defmodule MyStruct do defstruct data: %{} @behaviour Access # ... ...
New
snofang
In a typical business development task, having a function in a context module which accepts attributes of map type and passes them to Ect...
New
Oliver
One common problem we face in constructing lists is that there is (AFAIK) no support for conditionally inserting members into list declar...
New
bartblast
This could resolve to {[a: 1, b: 2]}. Was it ever considered to allow such syntax? Notice this: {:abc, a: 1, b: 2} and this: my_fun(:abc,...
New

Other popular topics Top

yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
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
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 42633 214
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
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
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
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement