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

jon
Some minor things in the paper edition that says “3 2020” on the title page verso, not mentioned in the book’s errata online: p. 186 But...
New
belgoros
Following the steps described in Chapter 6 of the book, I’m stuck with running the migration as described on page 84: bundle exec sequel...
New
raul
Hi Travis! Thank you for the cool book! :slight_smile: I made a list of issues and thought I could post them chapter by chapter. I’m rev...
New
conradwt
First, the code resources: Page 237: rumbl_umbrella/apps/rumbl/mix.exs Note: That this file is missing. Page 238: rumbl_umbrella/app...
New
alanq
This isn’t directly about the book contents so maybe not the right forum…but in some of the code apps (e.g. turbo/06) it sends a TURBO_ST...
New
brian-m-ops
#book-python-testing-with-pytest-second-edition Hi. Thanks for writing the book. I am just learning so this might just of been an issue ...
New
AndyDavis3416
@noelrappin Running the webpack dev server, I receive the following warning: ERROR in tsconfig.json TS18003: No inputs were found in c...
New
adamwoolhether
I’m not quite sure what’s going on here, but I’m unable to have to containers successfully complete the Readiness/Liveness checks. I’m im...
New
jwandekoken
Book: Programming Phoenix LiveView, page 142 (157/378), file lib/pento_web/live/product_live/form_component.ex, in the function below: d...
New
tkhobbes
After some hassle, I was able to finally run bin/setup, now I have started the rails server but I get this error message right when I vis...
New

Other popular topics Top

Devtalk
Hello Devtalk World! Please let us know a little about who you are and where you’re from :nerd_face:
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
PragmaticBookshelf
Rust is an exciting new programming language combining the power of C with memory safety, fearless concurrency, and productivity boosters...
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
AstonJ
Seems like a lot of people caught it - just wondered whether any of you did? As far as I know I didn’t, but it wouldn’t surprise me if I...
New
mafinar
Crystal recently reached version 1. I had been following it for awhile but never got to really learn it. Most languages I picked up out o...
New
OvermindDL1
Woooooooo! This is such a huge release for it, and 2 years incoming! In short, the library is now using an updated hyper backend (not j...
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
Help
I am trying to crate a game for the Nintendo switch, I wanted to use Java as I am comfortable with that programming language. Can you use...
New
AstonJ
If you want a quick and easy way to block any website on your Mac using Little Snitch simply… File > New Rule: And select Deny, O...
New

Latest in PragProg

View all threads ❯