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

abtin
page 20: … protoc command… I had to additionally run the following go get commands in order to be able to compile protobuf code using go...
New
telemachus
Python Testing With Pytest - Chapter 2, warnings for “unregistered custom marks” While running the smoke tests in Chapter 2, I get these...
New
jamis
The following is cross-posted from the original Ray Tracer Challenge forum, from a post by garfieldnate. I’m cross-posting it so that the...
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
gilesdotcodes
In case this helps anyone, I’ve had issues setting up the rails source code. Here were the solutions: In Gemfile, change gem 'rails' t...
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
leonW
I ran this command after installing the sample application: $ cards add do something --owner Brian And got a file not found error: Fil...
New
jskubick
I think I might have found a problem involving SwitchCompat, thumbTint, and trackTint. As entered, the SwitchCompat changes color to hol...
New
jskubick
I found an issue in Chapter 7 regarding android:backgroundTint vs app:backgroundTint. How to replicate: load chapter-7 from zipfile i...
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

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
malloryerik
Any thoughts on Svelte? Svelte is a radical new approach to building user interfaces. Whereas traditional frameworks like React and Vue...
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 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
Rainer
Not sure if following fits exactly this thread, or if we should have a hobby thread… For many years I’m designing and building model air...
New
dimitarvp
Small essay with thoughts on macOS vs. Linux: I know @Exadra37 is just waiting around the corner to scream at me “I TOLD YOU SO!!!” but I...
New
PragmaticBookshelf
Author Spotlight James Stanier @jstanier James Stanier, author of Effective Remote Work , discusses how to rethink the office as we e...
New
AstonJ
Was just curious to see if any were around, found this one: I got 51/100: Not sure if it was meant to buy I am sure at times the b...
New
PragmaticBookshelf
Author Spotlight Mike Riley @mriley This month, we turn the spotlight on Mike Riley, author of Portable Python Projects. Mike’s book ...
New
PragmaticBookshelf
Author Spotlight: Peter Ullrich @PJUllrich Data is at the core of every business, but it is useless if nobody can access and analyze ...
New

Sub Categories: