# How to deal with NULL value

**URL:** <https://discuss.rubyonrails.org/t/how-to-deal-with-null-value/33362>\
**Category:** rubyonrails-talk\
**Created:** [August 19, 2008, 6:22am UTC](https://discuss.rubyonrails.org/t/how-to-deal-with-null-value/33362 "2008-08-19T06:22:29Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [August 19, 2008, 6:22am UTC](https://discuss.rubyonrails.org/t/how-to-deal-with-null-value/33362/1 "2008-08-19T06:22:29Z")

</div>

I often run into a problem of getting error because on the NULL value in MySQL. One example is when asking for .length of a string or strftime for datetime. Would it be better not to use NULL as default value, for example strftime? What's the actual benefit of NULL?

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [August 19, 2008, 8:23am UTC](https://discuss.rubyonrails.org/t/how-to-deal-with-null-value/33362/2 "2008-08-19T08:23:06Z")

</div>

Well the beauty of ruby is that you could just define the method on nil to be a no-op (or even implement method\_missing on nil). Personally I think that's a terrible idea as it could so easily cover up mistakes: ruby has let you know that you're trying to do something nonsensical, ignore it at you peril

Fred

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [August 19, 2008, 9:13am UTC](https://discuss.rubyonrails.org/t/how-to-deal-with-null-value/33362/3 "2008-08-19T09:13:07Z")

</div>

> Frederick Cheung wrote:
> 
> > > I often run into a problem of getting error because on the NULL  
> > > value in MySQL. One example is when asking for .length of a string or  
> > > strftime for datetime. Would it be better not to use NULL as default value,  
> > > for example strftime? What's the actual benefit of NULL?
> > 
> > Well the beauty of ruby is that you could just define the method on nil to be a no-op (or even implement method\_missing on nil). Personally I think that's a terrible idea as it could so easily cover up mistakes: ruby has let you know that you're trying to do something nonsensical, ignore it at you peril
> > 
> > Fred
> 
> ( I realize now that this is more of a Ruby question. 🙂 )
> 
> How do I define the method on nil to be a no-op (and what do you mean with that)?

by no-op i mean 'does nothing'

class NilClass &nbsp;&nbsp;&nbsp;def foo &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nil &nbsp;&nbsp;&nbsp;end end

nil.foo #=\> nil

class NilClass &nbsp;&nbsp;&nbsp;def method\_missing(\*args) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nil &nbsp;&nbsp;&nbsp;end end

nil.bananarama #=\> nil

Of course this does mean that if you wrote @customer instead of  
@current\_customer or @logged\_in\_customer it would just fail silently  
instead of raising an error.

Fred

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [August 19, 2008, 9:19am UTC](https://discuss.rubyonrails.org/t/how-to-deal-with-null-value/33362/4 "2008-08-19T09:19:31Z")

</div>

Frederick Cheung wrote:

> class NilClass &nbsp;&nbsp;&nbsp;def foo &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nil &nbsp;&nbsp;&nbsp;end end
> 
> nil.foo #=\> nil
> 
> class NilClass &nbsp;&nbsp;&nbsp;def method\_missing(\*args) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nil &nbsp;&nbsp;&nbsp;end end
> 
> nil.bananarama #=\> nil

Thanks.

Probably a stupid question, but where do I add this in RoR? In the application\_controller or?

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [August 19, 2008, 9:45am UTC](https://discuss.rubyonrails.org/t/how-to-deal-with-null-value/33362/5 "2008-08-19T09:45:42Z")

</div>

> Frederick Cheung wrote:
> 
> > class NilClass &nbsp;&nbsp;def foo &nbsp;&nbsp;&nbsp;&nbsp;nil &nbsp;&nbsp;end end
> > 
> > nil.foo #=\> nil
> > 
> > class NilClass &nbsp;&nbsp;def method\_missing(\*args) &nbsp;&nbsp;&nbsp;&nbsp;nil &nbsp;&nbsp;end end
> > 
> > nil.bananarama #=\> nil
> 
> Thanks.
> 
> Probably a stupid question, but where do I add this in RoR? In the application\_controller or?

Well it could go anywhere as long as that somewhere is loaded by your  
app (and it needs to be at the top level of a file). I'd stick it in a  
file in lib and require it from an initializer (or rather I wouldn't  
do that at all because I think it's a terrible idea).

Fred

---

<div class="post-metadata">

**Author:** ![Mukund](https://avatars.discourse-cdn.com/v4/letter/m/e8c25b/32.png) [@Mukund](https://discuss.rubyonrails.org/u/Mukund)\
**Post date:** [August 19, 2008, 10:20am UTC](https://discuss.rubyonrails.org/t/how-to-deal-with-null-value/33362/6 "2008-08-19T10:20:37Z")

</div>

You can always check to see if the column returned a NULL value in Ruby using column.nil? or you can rescue the exception.

Example:

x=some\_function\_that\_fails\_on\_null\_parameter(NULL\_value\_column) rescue value\_in\_case\_of\_exception

It is safer to add exception handlers on a case-by-case basis than to generalize.

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [August 19, 2008, 10:42am UTC](https://discuss.rubyonrails.org/t/how-to-deal-with-null-value/33362/7 "2008-08-19T10:42:56Z")

</div>

Mukund wrote:

> You can always check to see if the column returned a NULL value in Ruby using column.nil? or you can rescue the exception.
> 
> Example:
> 
> x=some\_function\_that\_fails\_on\_null\_parameter(NULL\_value\_column) rescue value\_in\_case\_of\_exception
> 
> It is safer to add exception handlers on a case-by-case basis than to generalize.
> 
> On Aug 19, 11:22�am, P�l Bergstr�m \<rails-mailing-l...@andreas-s.net\>

I think I understand how to use column.nil?, but rescue is new to me. Is this correct?

column.length unless column.nil?

---

<div class="post-metadata">

**Author:** ![Mukund](https://avatars.discourse-cdn.com/v4/letter/m/e8c25b/32.png) [@Mukund](https://discuss.rubyonrails.org/u/Mukund)\
**Post date:** [August 19, 2008, 11:03am UTC](https://discuss.rubyonrails.org/t/how-to-deal-with-null-value/33362/8 "2008-08-19T11:03:30Z")

</div>

x = column.length unless column.nil? x = column.length rescue nil

will both give you the same results. Alternatively,

x = (column.nil?)? 0: column.length; x = column.length rescue 0

will give you zeroes instead of nil.

---

<div class="post-metadata">

**Author:** ![Mukund](https://avatars.discourse-cdn.com/v4/letter/m/e8c25b/32.png) [@Mukund](https://discuss.rubyonrails.org/u/Mukund)\
**Post date:** [August 19, 2008, 11:07am UTC](https://discuss.rubyonrails.org/t/how-to-deal-with-null-value/33362/9 "2008-08-19T11:07:04Z")

</div>

Just remember that in both Ruby and MySQL, NULL is not equal to FALSE.

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [August 19, 2008, 11:21am UTC](https://discuss.rubyonrails.org/t/how-to-deal-with-null-value/33362/10 "2008-08-19T11:21:42Z")

</div>

Mukund wrote:

> Just remember that in both Ruby and MySQL, NULL is not equal to FALSE.

So this will not work?

x = column.length unless column.nil?

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [August 19, 2008, 12:34pm UTC](https://discuss.rubyonrails.org/t/how-to-deal-with-null-value/33362/11 "2008-08-19T12:34:16Z")

</div>

Pål Bergström wrote:

> Mukund wrote:
> 
> > Just remember that in both Ruby and MySQL, NULL is not equal to FALSE.
> 
> So this will not work?
> 
> x = column.length unless column.nil?

I think the relevant question is what are you going to do with x?

Your code statement is valid, but think about what x is when column is nil, and how are you planning to use x?

The merits of setting x to zero when column.nil? as suggested by Mukund depends on what you're going to do with x.

---

<div class="post-metadata">

**Author:** ![Jorg\_Lueke](https://avatars.discourse-cdn.com/v4/letter/j/51bf81/32.png) [@Jorg\_Lueke](https://discuss.rubyonrails.org/u/Jorg_Lueke)\
**Post date:** [August 19, 2008, 1:05pm UTC](https://discuss.rubyonrails.org/t/how-to-deal-with-null-value/33362/12 "2008-08-19T13:05:35Z")

</div>

The benefit of a Null is that it doesn't figure in counts, maxes, mins, and other operations. It really means "I don't know" to the database and behaves accordingly. You can certainly default to other values like spaces or 0. Just know that the behavior is somewhat different.

You can always handle null, if string.length.nil? length = 0 if that's what you want

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [August 19, 2008, 1:11pm UTC](https://discuss.rubyonrails.org/t/how-to-deal-with-null-value/33362/13 "2008-08-19T13:11:16Z")

</div>

Mukund wrote:

> Just remember that in both Ruby and MySQL, NULL is not equal to FALSE.

I'm not sure you are saying what you intend to say. In Ruby, nil evaluates to false. In fact, only nil and false evaluate to false. Since Ruby uses nil instead of null, I assume you meant nil.

These two are functional equivalent:

do\_something if my\_var

do\_something unless my\_var.nil?

Peace, Phillip

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [August 19, 2008, 1:17pm UTC](https://discuss.rubyonrails.org/t/how-to-deal-with-null-value/33362/14 "2008-08-19T13:17:12Z")

</div>

Jorg Lueke wrote:

> You can always handle null, if string.length.nil? length = 0 if that's what you want

That's what I'm after. Thanks.

---

<div class="post-metadata">

**Author:** ![Jorg\_Lueke](https://avatars.discourse-cdn.com/v4/letter/j/51bf81/32.png) [@Jorg\_Lueke](https://discuss.rubyonrails.org/u/Jorg_Lueke)\
**Post date:** [August 19, 2008, 1:50pm UTC](https://discuss.rubyonrails.org/t/how-to-deal-with-null-value/33362/15 "2008-08-19T13:50:05Z")

</div>

Though it should be if string.nil? length = 0 string.length was your problem 🙂

---

<div class="post-metadata">

**Author:** ![Mukund](https://avatars.discourse-cdn.com/v4/letter/m/e8c25b/32.png) [@Mukund](https://discuss.rubyonrails.org/u/Mukund)\
**Post date:** [August 20, 2008, 9:09am UTC](https://discuss.rubyonrails.org/t/how-to-deal-with-null-value/33362/16 "2008-08-20T09:09:22Z")

</div>

I was referring to nil in Ruby and NULL in MySql. 🙂

nil as an object doesn't equate to false in Ruby.

irb(main):001:0\> x=nil =\> nil irb(main):002:0\> x==false =\> false irb(main):003:0\> x.nil? =\> true

You need to watch out for that while handling variables that can contain nil values in comparison statements. The my\_var.nil? function takes the nil object and gives you a boolean result.

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [August 20, 2008, 10:56am UTC](https://discuss.rubyonrails.org/t/how-to-deal-with-null-value/33362/17 "2008-08-20T10:56:08Z")

</div>

Mukund wrote:

> I was referring to nil in Ruby and NULL in MySql. 🙂
> 
> nil as an object doesn't equate to false in Ruby.

Ah, we're using different words. What you say is true. A nil object and a false object do not \_equate\_. But what I said is true as well. A nil object and a false object \_evaluate\_ to false in a boolean expression. Maybe not in a direct comparison as in your example, but definitely in the example I posted above.

> You need to watch out for that while handling variables that can contain nil values in comparison statements. The my\_var.nil? function takes the nil object and gives you a boolean result.

Agreed. When using variables in direct comparisons, definitely be careful of nil. But when testing a variable for whether it is or is not nil/false, just a simple

if my\_var

will suffice.

Peace, Phillip
