acke
Float comparision in ecto
Hi, I’m working through the PragProg “Programming Elixir LiveView” book and encountered a strange bug when implementing a changeset. The changeset is as follows:
def lower_price_changeset(product, new_price) do
old_price = product.unit_price
product
|> change(unit_price: new_price)
|> validate_number(:unit_price, less_than: old_price)
end
When I run the function using a product struct with the same unit price as new price, a valid schema where no changes are made is generated (see below). If I run the function using a new_price which is greater than product.unit_price, an invalid schema is generated so I assume that my logic is solid.
iex(52)> p
%Pento.Catalog.Product{
__meta__: #Ecto.Schema.Metadata<:built, "products">,
id: nil,
description: nil,
name: nil,
sku: nil,
unit_price: 9.0,
inserted_at: nil,
updated_at: nil
}
iex(53)> Pento.Catalog.Product.lower_price_changeset(p, 9.0)
#Ecto.Changeset<action: nil, changes: %{}, errors: [],
data: #Pento.Catalog.Product<>, valid?: true>
Am I missing something obvious or is this a bug? In that case, is there a workaround? I know that float comparison isn’t straightforward so perhaps this causes the issue?
Trending in Questions
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated!
To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead.
Sta...
New
Hello !
We want new/edit form pages to POST/PUT to their own URL rather than the resources REST defaults (post /things, put /things/:id)...
New
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app?
Looking for hints regarding:
Addi...
New
Hi all, I wanted to ask how the community is dealing with post-release steps.
Today we have Ecto migrations, which make sure that the db...
New
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #performance
- #security










First Post!
03juan
In the docs for
changeit saysChanged attributes will only be added if the change does not have the same value as the field in the data.So in this case I don’t think the validation is run because the unit price hasn’t changed.
Most Liked
03juan
@dimitarvp Something just came to mind…
changemay be the wrong function for this use case if we want the most idiomatic way to error for a dynamic test ofnew < oldwhen new could be == old.The docs also say about
change:But as we’ve seen the issue is that the change is not added when the values are the same.
I haven’t gone through the “Programming Phoenix LiveView” book in detail but I assume this is an exercise? All their changeset examples use
castinstead ofchangeand this is the most idiomatic approach to solve this problem, with an extra option.The reason to use cast is that if this changeset will be called with params from a form, it will attempt to cast the string value from the client into float, and return a cast error if that fails.
In which case we’d want to pass the params as an attribute to
cast03juan
Actually
put_changealso saysIf the change has the same value as in the changeset data, it is not added to the list of changes.I’ve checked the source for
validate_numberand it usesvalidate_changeinternally, which also states03juan
I really depends on your use case. What I’m assuming is you want the changeset to throw an error when
new >= old.It will throw an error when
new > oldbecausechange(unit_price: new_price)marksunit_priceas changed and thereforevalidate_number(:unit_price, less_than: old_price)runs against the changed value.But
changewon’t mark the attribute as changed ifnew = oldand therefore it won’t ever runvalidate_number.This is probably your best bet
because ecto not seeing the change is a feature to prevent unnecessary db writes when the value wouldn’t change, but you can still force it with
force_change.Otherwise you could add a custom error when the prices are equal and let the normal validation take care of the less_than case. (this is what I actually meant by custom validation, sorry for the confusion)
The changeset won’t show the price changed in the
changes:key, but it will have it in the error and the changeset won’t be valid, so you can adapt your business and application logic to match on it.Last Post!
03juan
It certainly does make them feel that way, and I’m also glad that we can discuss such things in these forums. At least the dev team is always open to contributions to improve the docs so I may give this a shot unless someone else gets inspired. If so ping me and we can work on it together