samedi 31 octobre 2015

Simple Mongoid typecast and update_attributes validation fails

I am trying to cast fields on mongoid documents. As you can see I am use float for my field When I update a persisted entry with a string, it overwrites my field with a zero value, which should stay the same. But more interesting is, that mongoid says true...

class Product
  include Mongoid::Document

  field :net_price, type: Float # cast for float not string
end

p = Product.create(net_price: 123.45)
# => #<Product _id: 5634bb2b34257be5e2000001, net_price: 123.45>

p.update_attributes!({net_price: "A String"})
# true

p.reload
# => #<Product _id: 5634bb2b34257be5e2000001, net_price: 0.0>

Aucun commentaire:

Enregistrer un commentaire