# ActiveRecord boolean false returning Nil

**URL:** <https://www.ruby-forum.com/t/activerecord-boolean-false-returning-nil/173255>\
**Category:** Rails\
**Created:** [August 5, 2009, 5:58pm UTC](https://www.ruby-forum.com/t/activerecord-boolean-false-returning-nil/173255 "2009-08-05T17:58:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![chris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@chris](https://www.ruby-forum.com/u/chris)\
**Post date:** [August 5, 2009, 5:58pm UTC](https://www.ruby-forum.com/t/activerecord-boolean-false-returning-nil/173255/1 "2009-08-05T17:58:38Z")

</div>

Hi All,

I have a column type of boolean, and when the value in that column is  
set to ‘false’ (eg. 0 for MySQL), it returns nil.

I have tested this in the console, and it doesn’t exhibit this  
behavior there, only when running on Mongrel.

I tried wrapping the accessor call to intercept a nil and set it to  
false for that attribute, but it doesn’t seem to be working. For  
Example:

def needs\_sound  
#ActiveRecord uses method\_missing to implement DB accessors, so we  
call super  
#to make it access the database.  
res = super  
if res == nil  
return false  
end  
return res  
end

The debugger says that ‘res’ is nil, but it ignores the conditional  
and returns ‘res’ anyway. I have tried the conditional as a  
‘res.nil?’, ‘res==nil’, ‘super == nil’.

Any help would be greatly appreciated. This is occurring for all  
boolean attributes when their value is false. Thanks,

Chris

---

<div class="post-metadata">

**Author:** ![chris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@chris](https://www.ruby-forum.com/u/chris)\
**Post date:** [August 5, 2009, 6:41pm UTC](https://www.ruby-forum.com/t/activerecord-boolean-false-returning-nil/173255/2 "2009-08-05T18:41:22Z")

</div>

> Any help would be greatly appreciated. This is occurring for all  
> boolean attributes when their value is false. Thanks,

That sounds like a feature. An unassigned boolean is indistinguishable  
from  
a false, so it’s a nil.

Well-written code never ever says ‘if bool == false’, which is probably  
why  
I have never seen a complaint.
