endymion

endymion

I have question on the limitations of embeds_many. I want non-homogenous embeds many.

Despite being able to define how to handle casting from the with option in embeds_many, casting fails when the type for the field is :map.

defmodule Container do
  use Ecto.Schema
  embedded_schema do
    embeds_many :foos_and_bars, :map
  end
end

defmodule Foo do
  use Ecto.Schema
  embedded_schema do
    field :position, :integer
    field :unique_data, :list
  end
end

defmodule Bar do
  use Ecto.Schema
  embedded_schema do
    field :position, :integer
    field :more_unique_data, :bool
  end

The issue arises when I need to call cast_embed on the Container to validate foos_and_bars

container = %Container{foos_and_bars: [%Bar{position: 1, more_unique_data: true}]}
params = %{
  foos_and_bars: %{
    "1" => %{more_unique_data: false}
}}

Ecto.Changeset.cast(container, params, [])
|> Ecto.Changeset.cast_embed(:foos_and_bars, with: fn foo_or_bar, params ->
  case foo_or_bar do
    foo = %Foo{} -> Foo.changeset(foo, params)
    bar = %Bar{} -> Foo.changeset(bar, params)
  end
end)

The cast_embed throws (UndefinedFunctionError) function :map.__schema__/1 is undefined (module :map is not available) :map.__schema__(:primary_key) as it expects the field to be a schema not just a plain :map.

My question is why is this the case? I understand that there may not be an obvious use for field backed by a database to need this type flexiblity but when changesets are the first class supported method of input validation in Phoenix these issues arrive. The wider context is for my application we have an ordered list of inputs. Some those “inputs” are nested input lists. To maintain the order correctly inside of the inputs_for we need to store an Enumerable with two types (a plain input or nested group of inputs). Problems arrise when casting the changeset as described above.

I can further understand why this error could get thrown if Ecto.Changeset.cast_embed was not called alongside the with option. The with allows you to define how to handle casting for each item in the embeds. which in my opinion should allow me to have case that matches on type and delegates to those modules changeset functions.

embeds_many :foos_and_bars, :map should either raise an exception or the case should be handled as described above.

Showing Posts 1 to 2

al2o3cr

al2o3cr

This sounds like exactly what polymorphic_embed was designed to do:

Without the “type” field that that uses, it’s not possible to figure out which schema the results should be when retrieving a schema that says embeds_many

If you really care exclusively about writing these shapes, consider saying field :foos_and_bars, :map and then manually using Foo and Bar’s changesets to produce maps in cast_embed.

endymion

endymion OP

Nice library, interesting that Ecto doesn’t support this behavior itself though. The polymorphic embeds inputs_for is a nice feature to support as well to easily switch off type. That is my exact use case.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews