# ActiveRecord::ConnectionAdapters::Column value\_to\_boolean(value) does not return a boolean value

**URL:** <https://www.ruby-forum.com/t/activerecord-column-value-to-boolean-value-does-not-return-a-boolean-value/194374>\
**Category:** Rails\
**Created:** [September 3, 2010, 10:29am UTC](https://www.ruby-forum.com/t/activerecord-column-value-to-boolean-value-does-not-return-a-boolean-value/194374 "2010-09-03T10:29:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![powertoaster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b5ac83/32.png) [@powertoaster](https://www.ruby-forum.com/u/powertoaster)\
**Post date:** [September 3, 2010, 10:29am UTC](https://www.ruby-forum.com/t/activerecord-column-value-to-boolean-value-does-not-return-a-boolean-value/194374/1 "2010-09-03T10:29:50Z")

</div>

value\_to\_boolean(value), does not always return a boolean value, only  
when true.

150: def value\_to\_boolean(value)  
151: if value.is\_a?(String) && value.blank?  
152: nil  
153: else  
154: TRUE\_VALUES.include?(value)  
155: end  
156: end

If value is contained in TRUE\_VALUES then it returns true(TrueClass).  
But if value is anything else it returns nil(NilClass) instead of  
false(FalseClass)

This does not seem consistent or expected to me since false \<\> nil.

Since we have TRUE\_VALUES and FALSE\_VALUES, I think it should be  
something like this.

150 def value\_to\_boolean(value)  
151 if TRUE\_VALUES.include?(value)  
152 true  
153 elsif FALSE\_VALUES.include?(value)  
154 false  
155 else  
156 nil  
157 end  
158 end

---

<div class="post-metadata">

**Author:** ![powertoaster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b5ac83/32.png) [@powertoaster](https://www.ruby-forum.com/u/powertoaster)\
**Post date:** [September 3, 2010, 4:00pm UTC](https://www.ruby-forum.com/t/activerecord-column-value-to-boolean-value-does-not-return-a-boolean-value/194374/2 "2010-09-03T16:00:37Z")

</div>

On Sep 2, 9:21 pm, powertoaster [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> If value is contained in TRUE\_VALUES then it returns true(TrueClass).  
> But if value is anything else it returns nil(NilClass) instead of  
> false(FalseClass)
> 
> This does not seem consistent or expected to me since false \<\> nil.
> 
> Since we have TRUE\_VALUES and FALSE\_VALUES, I think it should be  
> something like this.

The code as it is ensures that if the column contains NULL then you  
get nil out. 3 way true/false/unset logic can be a little odd but I’m  
not sure whether it should just be completely supressed.  
It might also be misleading in that foo.flag == false leads you to  
believe that Foo.find\_all\_by\_flag(false) would return that row, which  
it wouldn’t if the flag is NULL. If you don’t like NULLs in boolean  
columns, then make it a non null column (which i usually do)

Fred
