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.
Popular in Proposals: Ideas
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
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
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
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
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
Ecto currently supports some data-modifying WITH statements / CTEs for Postgres:
Options: […]
:operation - one of :all , :update_all ,...
New
I implemented Access behavior for a struct today. Pseudo-code…
defmodule MyStruct do
defstruct data: %{}
@behaviour Access
# ... ...
New
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
One common problem we face in constructing lists is that there is (AFAIK) no support for conditionally inserting members into list declar...
New
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
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
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
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
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
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
New
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
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
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
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
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







