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

manhvu
In a large repo, working with module need to add alias too much is quite annoyed and not good for organizing code. I think better add su...
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
rhcarvalho
Hi all, I would like to gather some feedback before a more intentional proposal to add a new :depth option when specifying a Git depende...
New
cevado
Being able to build nested relations in a schemaless changeset would be helpful to deal with complex forms on Phoenix without the need to...
New
mikesax
I was writing code that included a function I would call later (so it wasn’t called yet). I got a warning, and I thought I could put an u...
New
ffloyd
The Problem Currently, if I define a struct in the following way: defmodule MyStruct do # Both x and y will have the FIXED values unti...
New
7rans
I implemented Access behavior for a struct today. Pseudo-code… defmodule MyStruct do defstruct data: %{} @behaviour Access # ... ...
New
pejrich
I propose adding compact_map/2 to the Enum module. What is it? Sometimes you want to map over a collection, but sometimes you want to ma...
New
sezaru
When writing my code, I always find __MODULE__ very useful to use as alias of that module “inner dependencies”, ex: alias __MODULE__.{Im...
New

Other popular topics Top

peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
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
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
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
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
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
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

We're in Beta

About us Mission Statement