Close
Glad You're Ready. Let's Get Started!

Let us know how we can contact you.

Thank you!

We'll respond shortly.

PIVOTAL LABS
Standup 03/04/2009

Interesting Things

Integer("008") != "008".to_i
  • The to_i method is what you want, unless you want exceptions or octal numbers.

  • Somebody needed help constructing a named_scope where they could reference the count of an associated has_many association. There was some grumbling about using :joins and :group (and if you do this, be sure not to call count on the scope itself without also doing a :select => 'DISTINCT primary_key'). The winning solution was to just put a counter_cache on the association and use the denormalized column instead.

Comments
  1. Erik Hanson says:

    Javascript’s parseInt() has a similar issue:


    parseInt("8") === 8

    parseInt("08") === 0

    parseInt("010") === 8

    One solution is to include the radix:


    parseInt("08", 10) === 8

    Or use the Number function or constructor:


    Number("08") === 8

    new Number("08") === 8

    Another trick is to convert the string using the + operator:


    +"08" === 8

Post a Comment

Your Information (Name required. Email address will not be displayed with comment.)

* Copy This Password *

* Type Or Paste Password Here *