IdiosApps

IdiosApps

P85 Your Turn - (Ecto.CastError) expected params to be a :map, got: `3.0`

Generators: Contexts and Schemas - p85

Here is the code I have:

catalog.ex:

  def markdown_product(%Product{} = product, unit_price) do
    product
    |> Product.changeset_price(unit_price)
    |> Repo.update()
  end

product.ex:

  def changeset_price(product, unit_price) do
    product
    |> cast(unit_price, [:unit_price])
    |> validate_number(:unit_price, greater_than: 0.0)
  end

My commands:

# mix priv/generators_cli_commands.exs
# or `iex -S mix`, then enter line-by-line

alias Pento.Catalog

random_sku = Enum.random(0 .. 1_000_000)
product_attrs = %{
    name: "Chess",
    description: "The classic strategy game",
    unit_price: 10.0,
    sku: random_sku,
}

{:ok, product} = Catalog.create_product(product_attrs)

unit_price = 3.0

Catalog.markdown_product(product, unit_price)

It runs OK until the last line, which gives:

** (Ecto.CastError) expected params to be a :map, got: `3.0`

There is some generated @spec, but it doesn’t mean much to me right now:

  @spec markdown_product(
          %Pento.Catalog.Product{optional(atom) => any},
          :invalid | %{optional(:__struct__) => none, optional(atom | binary) => any}
        ) :: any

A few questions:

  • How can I tell that it expects a :map for unit_price?
    • Why does it expect a map?
  • How can I tell Elixir that I expect this second paramter to be a float?

Thanks!

Popular Prag Prog topics Top

Razor54672
The answer to 3rd Problem of Chapter 5 (Making Choices) of “Practical Programming, Third Edition” seems incorrect in the given answer ke...
New
mikecargal
Title: Hands-On Rust (Chapter 11: prefab) Just played a couple of amulet-less games. With a bit of debugging, I believe that your can_p...
New
herminiotorres
Hi @Margaret , On page VII the book tells us the example and snippets will be all using Elixir version 1.11 But on page 3 almost the en...
New
herminiotorres
Hi! I know not the intentions behind this narrative when called, on page XI: mount() |> handle_event() |> render() but the correc...
New
cro
I am working on the “Your Turn” for chapter one and building out the restart button talked about on page 27. It recommends looking into ...
New
adamwoolhether
When trying to generate the protobuf .go file, I receive this error: Unknown flag: --go_opt libprotoc 3.12.3 MacOS 11.3.1 Googling ...
New
digitalbias
Title: Build a Weather Station with Elixir and Nerves: Problem connecting to Postgres with Grafana on (page 64) If you follow the defau...
New
oaklandgit
Hi, I completed chapter 6 but am getting the following error when running: thread 'main' panicked at 'Failed to load texture: IoError(O...
New
akraut
The markup used to display the uploaded image results in a Phoenix.LiveView.HTMLTokenizer.ParseError error. lib/pento_web/live/product_l...
New
creminology
Skimming ahead, much of the following is explained in Chapter 3, but new readers (like me!) will hit a roadblock in Chapter 2 with their ...
New

Other popular topics Top

AstonJ
If it’s a mechanical keyboard, which switches do you have? Would you recommend it? Why? What will your next keyboard be? Pics always w...
New
DevotionGeo
I know that these benchmarks might not be the exact picture of real-world scenario, but still I expect a Rust web framework performing a ...
New
Exadra37
I am thinking in building or buy a desktop computer for programing, both professionally and on my free time, and my choice of OS is Linux...
New
DevotionGeo
I know that -t flag is used along with -i flag for getting an interactive shell. But I cannot digest what the man page for docker run com...
New
AstonJ
I ended up cancelling my Moonlander order as I think it’s just going to be a bit too bulky for me. I think the Planck and the Preonic (o...
New
rustkas
Intensively researching Erlang books and additional resources on it, I have found that the topic of using Regular Expressions is either c...
New
PragmaticBookshelf
Rails 7 completely redefines what it means to produce fantastic user experiences and provides a way to achieve all the benefits of single...
New
AstonJ
If you get Can't find emacs in your PATH when trying to install Doom Emacs on your Mac you… just… need to install Emacs first! :lol: bre...
New
New
PragmaticBookshelf
Author Spotlight Erin Dees @undees Welcome to our new author spotlight! We had the pleasure of chatting with Erin Dees, co-author of ...
New

Latest in PragProg

View all threads ❯