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!

Where Next?

Popular Pragmatic Bookshelf topics Top

ianwillie
Hello Brian, I have some problems with running the code in your book. I like the style of the book very much and I have learnt a lot as...
New
Charles
In general, the book isn’t yet updated for Phoenix version 1.6. On page 18 of the book, the authors indicate that an auto generated of ro...
New
brunogirin
When trying to run tox in parallel as explained on page 151, I got the following error: tox: error: argument -p/–parallel: expected one...
New
brunogirin
When running tox for the first time, I got the following error: ERROR: InterpreterNotFound: python3.10 I realised that I was running ...
New
AufHe
I’m a newbie to Rails 7 and have hit an issue with the bin/Dev script mentioned on pages 112-113. Iteration A1 - Seeing the list of prod...
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
kolossal
Hi, I need some help, I’m new to rust and was learning through your book. but I got stuck at the last stage of distribution. Whenever I t...
New
mert
AWDWR 7, page 152, page 153: Hello everyone, I’m a little bit lost on the hotwire part. I didn’t fully understand it. On page 152 @rub...
New
Henrai
Hi, I’m working on the Chapter 8 of the book. After I add add the point_offset, I’m still able to see acne: In the image above, I re...
New
SlowburnAZ
Getting an error when installing the dependencies at the start of this chapter: could not compile dependency :exla, "mix compile" failed...
New

Other popular topics Top

ohm
Which, if any, games do you play? On what platform? I just bought (and completed) Minecraft Dungeons for my Nintendo Switch. Other than ...
New
wolf4earth
@AstonJ prompted me to open this topic after I mentioned in the lockdown thread how I started to do a lot more for my fitness. https://f...
New
Exadra37
Please tell us what is your preferred monitor setup for programming(not gaming) and why you have chosen it. Does your monitor have eye p...
New
AstonJ
You might be thinking we should just ask who’s not using VSCode :joy: however there are some new additions in the space that might give V...
New
AstonJ
I’ve been hearing quite a lot of comments relating to the sound of a keyboard, with one of the most desirable of these called ‘thock’, he...
New
AstonJ
Do the test and post your score :nerd_face: :keyboard: If possible, please add info such as the keyboard you’re using, the layout (Qw...
New
AstonJ
We’ve talked about his book briefly here but it is quickly becoming obsolete - so he’s decided to create a series of 7 podcasts, the firs...
New
PragmaticBookshelf
Author Spotlight: VM Brasseur @vmbrasseur We have a treat for you today! We turn the spotlight onto Open Source as we sit down with V...
New
PragmaticBookshelf
Author Spotlight: Tammy Coron @Paradox927 Gaming, and writing games in particular, is about passion, vision, experience, and immersio...
New
AstonJ
If you’re getting errors like this: psql: error: connection to server on socket “/tmp/.s.PGSQL.5432” failed: No such file or directory ...
New

Sub Categories: