I’m trying to figure out how to use cast_embed but I don’t understand what I’m doing wrong.
defmodule Operation do
use Ecto.Schema
embedded_schema do
field :amount, :integer
field :type, :string
embeds_one :item, Item
end
end
defmodule Item do
use Ecto.Schema
embedded_schema do
field :item_code, :string
field :item_name, :string
end
end
:with - the function to build the changeset from params. Defaults to the changeset/2 function in the embed module
Basically, when you call cast_embed, it implicitly calls Item.changeset/2, but since you don’t have one defined its erroring out. You want to add a def changeset(changeset, params) function in your Item module
Thanks, can you give an example of how that would be done with my above structs?
I’m still trying to get it working but frustratingly just getting errors…
defmodule Item do
use Ecto.Schema
embedded_schema do
field :item_code, :string
field :item_name, :string
end
def changeset(schema, params) do
schema
|> cast(params, [:item_code, :item_name])
end
end