wolfiton
Hi everyone,
So following the absinthe book I arrived at the subscription chapter with the ordering system powered by embedded schemas.
My problem is that cast/4 is deprecated but I couldn’t find any example of cast_embed/3
My Order schema looks like this:
defmodule PlateSlate.Ordering.Order do
use Ecto.Schema
import Ecto.Changeset
alias PlateSlate.Ordering.Order
schema "orders" do
field :customer_number, :integer, read_after_writes: true
field :ordered_at, :utc_datetime, read_after_writes: true
field :state, :string, read_after_writes: true
embeds_many :items, PlateSlate.Ordering.Item
timestamps()
end
@doc false
def changeset(%Order{} = order, attrs) do
order
|> cast(attrs, [:customer_number, :items, :ordered_at, :state])
|> cast_embed(:items)
end
end
My Item schema looks like this:
defmodule PlateSlate.Ordering.Item do
use Ecto.Schema
import Ecto.Changeset
embedded_schema do
field :price, :decimal
field :name, :string
field :quantity, :integer
end
def changeset(item, attrs) do
item
|> cast(attrs, [:price, :name, :quantity])
|> validate_required([:price, :name, :quantity])
end
end
Error trace:
mix test test/plate_slate/ordering_test.exs 1 ✘ at 17:23:35
1) test orders create_order/1 with valid data creates a order (PlateSlate.OrderingTest)
test/plate_slate/ordering_test.exs:13
** (RuntimeError) casting embeds with cast/4 for :items field is not supported, use cast_embed/3 instead
code: assert {:ok, %Order{} = order} = Ordering.create_order(attrs)
stacktrace:
(ecto) lib/ecto/changeset.ex:545: Ecto.Changeset.type!/2
(ecto) lib/ecto/changeset.ex:519: Ecto.Changeset.process_param/7
(elixir) lib/enum.ex:1948: Enum."-reduce/3-lists^foldl/2-0-"/3
(ecto) lib/ecto/changeset.ex:504: Ecto.Changeset.cast/6
(plate_slate) lib/plate_slate/ordering/order.ex:17: PlateSlate.Ordering.Order.changeset/2
(plate_slate) lib/plate_slate/ordering.ex:56: PlateSlate.Ordering.create_order/1
test/plate_slate/ordering_test.exs:24: (test)
Finished in 0.1 seconds
1 test, 1 failure
Also, I don’t have any embeds in my test file, so the error is definitely coming from my schemas, just wondering what is the correct way to define cast_embed/3
Because I think that how it is now it should be correct.
Thanks in advance
Trending in Chat/Questions
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
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
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
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
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 4- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
fuelen
Hello!
Just remove
:itemsfrom the list of attributes oncastcall.wolfiton
Thanks @fuelen,
That is the solution.
Can I also add that the error I got is misleading and doesn’t explain what is really happening?
Thanks again for the help
Adzz
I think the error makes sense if you know what it’s saying.
Items is an embed, and the error is telling you you tried to cast an embedded field in cast/4, which is not allowed.
Even though you did a cast_embed after, it hits this error first.
wolfiton
Thanks @Adzz for the follow up on this.
But I think that a more clearer way to define this error should be like this:
I think that this should make more sense to everybody and also an exmaple in the docs.