#1180 ✓not-applicable
Kai Chen

should allow assign a empty string to numeric property

Reported by Kai Chen | January 13th, 2010 @ 03:34 AM

More cases in web development will assign hash from request params to model for create or update.

And empty string could convert to numeric zero:

  "".to_f #=> 0.0
  "".to_i #=> 0

So I think dm should allow assign a empty string to numeric property, such as Float or Integer.

test script:
http://gist.github.com/276068

patch:
http://github.com/kaichen/dm-core/commit/33fe06f11a0a893ee0cb30ef31...

Comments and changes to this ticket

  • Dan Kubb (dkubb)

    Dan Kubb (dkubb) February 2nd, 2010 @ 12:32 PM

    • State changed from “new” to “not-applicable”

    Unfortunately this type of coercion will not be handled by DataMapper. DM will only be coercing strings that are directly representations of an Integer or Float, so "0" and "0.0" respectively.

    I know that coercing a blank string into 0 is how String#to_f and String#to_i do things, but I think that behaviour only makes sense in a narrow set of cases. There are many cases where a blank String would be invalid, and coercing it automatically would hide errors. DM errs on the side of being more strict because it can't make any assumptions about what environment it's being used in.

    BTW: Ruby has two methods that are specifically meant to handle coercion: Integer() and Float(). Neither of those work like String#to_f and String#to_i when given a blank string.

  • Dan Kubb (dkubb)

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

People watching this ticket

Pages