wolfiton

wolfiton

Craft graphql api with Absinthe book- Subscriptions chapter 6 place orders

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

Most Liked

fuelen

fuelen

Hello!
Just remove :items from the list of attributes on cast call.

Adzz

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.

Where Next?

Popular in Chat/Questions Top

pdgonzalez872
Do we have a list of academic/research papers: about Elixir/Erlang? that use Elixir/Erlang? about the Beam? If so, could you please po...
New
yachnytskyi
Hello everyone. I am gonna start with Elixir/Phoenix, thinking to use Stephen Grider as a start point, then elixir school and other sourc...
New
shansiddiqui94
Hello all, I recently did my first app in Phoenix and Liveview, many thanks to all the users who assisted me. I found that the tutorial ...
New
armanm
I know zero downtime deployment can mean different things depending on your application and what your users can tolerate so expectations ...
New
Scoty
Hey, I am currently reading Programming Elixir and I am doing one of the exercises where you should write a solution to the “I’m thinkin...
New
Fl4m3Ph03n1x
GenStage and Flow resources? I have been hearing about GenStage and Flow, a tool built upon it, for quite some time. I understand it allo...
New
gouvermxt
I just finished the “Learn Functional Programming with Elixir (Pragprog)” book. I have 5+ years of experience with Ruby/Rails, my goal is...
New
InkFlo
Hi everyone, This year I’m graduated from Bachelor Degree (in computer science) from France (not really a bachelor, the exact term is “L...
New
tom_s
Hello Elixir Community! I’m new to functional programming in general and to Elixir in particular but I’m very intrigued and would like t...
New
imanuelgittens
Hey all! Just joined the forum and I’m very excited to learn the language. I just finished reading the documentation and I was wondering ...
New

Other popular topics Top

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
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
aesmail
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
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36128 110
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

We're in Beta

About us Mission Statement