Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Allow Infinity to pass validates_numericality_of

#354

This patch allows a getter method on a model to return infinity and still pass validates_numericality_of.

This patch has been running in a production app since January 2008.

Why is Infinity useful?

Ruby's Infinity (1.0/0.0) can be very useful when implementing "limits" in a model. For example a package for an account might have a page_limit specifying how many pages a user might be able to create. If you wish to support "unlimited" size packages you might allow page_limit to be stored as a NULL in the database, and have a getter method that returns Infinity when the attribute is nil. This allows you to use the attribute directly in comparisons and other kinds of basic math like "remaining pages" without needing conditional logic.

Reported by Chris Cherry · June 6th, 2008 @ 07:38 AM

State: resolved
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. shuber
    shuber

    Seems like it could be pretty useful in some situations.

    Applied the patch, works as expected and all tests pass.

    +1

    June 6th, 2008 @ 10:29 PM

  2. Gaius Centus Novus
    Gaius Centus Novus

    +1 all tests pass; tests are easy to understand

    June 13th, 2008 @ 10:38 AM

  3. Lytol
    Lytol

    +1 Putting mathematical debates aside (since infinity is not a number), I can see the usefulness of this functionality. Tests pass... let's see what others have to say. With mathematical theory aside, a pretty clever solution. =)

    June 18th, 2008 @ 07:45 AM

  4. Chris Cherry
    Chris Cherry
    • No changes were found…

    June 18th, 2008 @ 10:35 PM

  5. Repository
    Repository
    • State changed from new to resolved
    • Tag set to patch, tiny, verified

    (from [124d1016fa212c008e33853912493fa9ac15d086]) Allow Infinity (1.0/0.0) to pass validates_numericality_of. [#354 state:resolved]

    Signed-off-by: Pratik Naik

    http://github.com/rails/rails/co...

    July 9th, 2008 @ 01:25 PM

  6. Repository
    Repository

    (from [d12660022525b9b1e25b92cd4639e32c8608f729]) Allow Infinity (1.0/0.0) to pass validates_numericality_of. [#354 state:resolved]

    Signed-off-by: Pratik Naik

    http://github.com/rails/rails/co...

    July 15th, 2008 @ 11:56 PM