<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/stylesheets/rss.css" type="text/css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>I.NFECTIO.US: Defining Numericality</title>
    <link>http://i.nfectio.us/articles/2006/01/27/defining-numericality</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>"Nothing in life is worth doing if you have no life while doing it"</description>
    <item>
      <title>Defining Numericality</title>
      <description>&lt;p&gt;Now that I've been promoted to a management position, I don't get the opportunity to program as much as I'd like. I love sharing my knowledge of something I am passionate about and feel obligated to repay the debt I accumlated when I was learning how to program 10 years ago.
So I monitor the Ruby on Rails mailing list for people in distress and throw on my cape and try to come to their rescue.
&lt;/p&gt;
&lt;p&gt;I had just such an opportunity last week, when this distressed soul wanted to validate that an input field was a positive integer.&lt;/p&gt;
&lt;p&gt;This should be an easy one to figure out. validates_numericality_of sounds like the perfect remedy. As it turns out, validates_numericality_of only determines if its input is indeed a number. The only constraint you can specify is that its an integer as opposed to a decimal value.&lt;/p&gt;
&lt;p&gt;My other choice would be validates_inclusion_of. Using the :in parameter to that method allows me to constrain the input value. I can specify that I want it to be within 0 and 4294967295.&lt;/p&gt;&lt;br /&gt;
&lt;code&gt;validates_inclusion_of :int_field, :in =&gt; 0..4294967295&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;
&lt;p&gt;Not very pretty or intuitive. I got to wondering why this functionality couldn't be included in the validates_numericality_of routine. 'Numericality' is made up of 2 parts. 'Numerical' and 'ity'. We all know what 'Numerical' means (of or relating to numbers) and 'ity' is used to convert an adjective to a noun. Basically, it means we have a number. Therefore, 'Numericality' doesn't inherently constrain the &lt;em&gt;value&lt;/em&gt; of a number, and in my opinion, neither should validates_numericality_of.&lt;/p&gt;&lt;br /&gt;
&lt;code&gt; :validates_numericality_of :my_positive_integer, :gte =&gt; 0 &lt;/code&gt;&lt;br /&gt;&lt;br /&gt;
&lt;p&gt;Problem solved. You can download this &lt;a href="http://www.railtie.net/plugins/validates_numericality_of-0.1.zip"&gt;plugin&lt;/a&gt;, which extends validates_numericality_of with 7 new options:

&lt;ul&gt;
&lt;li&gt;:gt - Greater than&lt;/li&gt;
&lt;li&gt;:gte - Greater than or equal&lt;/li&gt;
&lt;li&gt;:eq - Equal to&lt;/li&gt;
&lt;li&gt;:lt - Less than&lt;/li&gt;
&lt;li&gt;:lte - Less than or equal&lt;/li&gt;
&lt;li&gt;:even - Even number&lt;/li&gt;
&lt;li&gt;:odd - Odd number&lt;/li&gt;
&lt;/ul&gt;

&lt;/p&gt;

&lt;p&gt;Another lost soul rescued and now I must hang up my cape, return to my regular job and wait for another opportunity to jump into action.&lt;/p&gt;

&lt;p&gt;
&lt;strong&gt;UPDATE: This has been submitted as a patch against trunk.&lt;/strong&gt;&lt;br /&gt;
&lt;a href="http://dev.rubyonrails.org/ticket/3952"&gt;http://dev.rubyonrails.org/ticket/3952&lt;/a&gt;&lt;/p&gt;</description>
      <pubDate>Fri, 27 Jan 2006 10:56:00 -0800</pubDate>
      <guid isPermaLink="false">urn:uuid:d0349f60f72a2f8815fb4d665535ec82</guid>
      <author>Bob Silva</author>
      <link>http://i.nfectio.us/articles/2006/01/27/defining-numericality</link>
      <category>Ruby on Rails</category>
    </item>
    <item>
      <title>"Defining Numericality" by PMc</title>
      <description>There is some strangeness here:&lt;br /&gt;
1.&lt;br /&gt;
A not-equal operator is missing. I do not see the real sense in an equal operator - if there should be a predefined specific value for a field, then why open a field at all? But I see lot of sense in a non-equal operator, especially in each shopping system you do not want an item-count 0 entered.&lt;br /&gt;
2.&lt;br /&gt;
There is no longer checked against general numericality as soon as there is a comparison added. I do not see why an erroneous entry like maybe "19%0" should still be compared but not be detected as erroneous.&lt;br /&gt;
3.&lt;br /&gt;
If I request ":gte =&gt; 2.5" and enter 2.5, I still get an error. This is obvious from the code, as there is a conversion to_i done. I do not see why.&lt;br /&gt;
4.&lt;br /&gt;
As soon as one of the comparisons is used, the error message given by :message is no longer used, instead a static one from the plugin is used.&lt;br /&gt;
5. &lt;br /&gt;
I do not understand the code.</description>
      <pubDate>Thu, 22 Feb 2007 15:07:16 -0800</pubDate>
      <guid isPermaLink="false">urn:uuid:06755bf3-130e-406e-8176-4826cf1fb561</guid>
      <link>http://i.nfectio.us/articles/2006/01/27/defining-numericality#comment-265</link>
    </item>
    <item>
      <title>"Defining Numericality" by yves_dufour@mac.com</title>
      <description>Why it's not possible to write ?

:validates_numericality_of :my_integer1, :gte = 0
:validates_numericality_of :my_integer2, :gte = :my_integer1

would be a must....

yd
 </description>
      <pubDate>Wed, 11 Oct 2006 08:16:30 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:a0978e76-adaf-4f70-aee5-43a5ac812ea9</guid>
      <link>http://i.nfectio.us/articles/2006/01/27/defining-numericality#comment-188</link>
    </item>
    <item>
      <title>"Defining Numericality" by Problems</title>
      <description>I am running into trouble with the 'gte' and 'lte' operators somehow they fail to work. The error message doesn't seem to be added to the error messages for those operators. I am not sure what exactly is the problem. The other operators work just fine.</description>
      <pubDate>Tue, 22 Aug 2006 02:15:00 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:6b35f499-1f86-4a89-82ff-19464a1caa96</guid>
      <link>http://i.nfectio.us/articles/2006/01/27/defining-numericality#comment-58</link>
    </item>
    <item>
      <title>"Defining Numericality" by Awesome!</title>
      <description>Thanks!

I came across the RoR mailing list post about this first before stumbling upon this article from a slightly different Google search.</description>
      <pubDate>Mon, 31 Jul 2006 19:36:02 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:de62c92d-85e5-4052-ae68-d20ad57fbac0</guid>
      <link>http://i.nfectio.us/articles/2006/01/27/defining-numericality#comment-41</link>
    </item>
    <item>
      <title>"Defining Numericality" by Great!</title>
      <description>Thanks for this simple plugin, it's really cool!</description>
      <pubDate>Mon, 31 Jul 2006 13:23:36 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:76d4be3c-7eaf-425c-9924-04aece757a74</guid>
      <link>http://i.nfectio.us/articles/2006/01/27/defining-numericality#comment-40</link>
    </item>
  </channel>
</rss>
