# Validates\_inclusion\_of on boolean within file fails

**URL:** <https://www.ruby-forum.com/t/validates-inclusion-of-on-boolean-within-file-fails/181518>\
**Category:** Rails\
**Created:** [January 13, 2010, 1:19am UTC](https://www.ruby-forum.com/t/validates-inclusion-of-on-boolean-within-file-fails/181518 "2010-01-13T01:19:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [January 13, 2010, 1:19am UTC](https://www.ruby-forum.com/t/validates-inclusion-of-on-boolean-within-file-fails/181518/1 "2010-01-13T01:19:29Z")

</div>

Well I’m making a lot of headway with reading an uploaded yml file that  
contains values that are read and then inserted into the model. The  
formatted .yml file now looks like this:

theme:  
name: My Theme  
author: John D.  
date: 2010-01-01  
stylesheets: true  
javascripts: false  
layouts: true  
swfs: true

I’m reading all of these yml values through a file upload which stores  
it into a hash.

When saving the values to my database, all of the validators work  
perfectly with the exception of the boolean reads.

validates\_inclusion\_of :stylesheets, :javascripts, :layouts, :swfs, :in  
=\> [true, false]

If it contains true it does not fail and stores the value as true.  
If it contains false it does not fail and stores the value as false.  
If it contains nothing it fails and says that it must contain a boolean  
value.

All good right?

If it contains anything “booga, dodo, ugha, etc.” it stores it as  
“false” and does not fail. This is my problem.

theme:  
name: failure  
author: John D.  
date: 2010-01-01  
stylesheets: true  
javascripts: booga  
layouts: true  
swfs: true

This will validate fine and store the javascripts as a boolean for  
false. Why does this validation not work with this?

Thanks.

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [January 13, 2010, 6:03pm UTC](https://www.ruby-forum.com/t/validates-inclusion-of-on-boolean-within-file-fails/181518/2 "2010-01-13T18:03:33Z")

</div>

On Jan 12, 7:19Â pm, Alpha B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> If it contains true it does not fail and stores the value as true.  
> If it contains false it does not fail and stores the value as false.  
> If it contains nothing it fails and says that it must contain a boolean  
> value.
> 
> All good right?
> 
> If it contains anything “booga, dodo, ugha, etc.” it stores it as  
> “false” and does not fail. Â This is my problem.

It’s likely you’re having an issue with the AR typecasting code; the  
relevant bit is from schema\_definitions.rb:

```
    def value_to_boolean(value)
      if value.is_a?(String) && value.blank?
        nil
      else
        TRUE_VALUES.include?(value)
      end
    end

```

Essentially, anything that isn’t blank and isn’t “true-y” (see the  
list in the source) gets mapped to false.

About the only way I can think of to avoid this is to override the  
accessors for those variables and map everything that isn’t true to  
nil.

–Matt J.

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [January 13, 2010, 9:48pm UTC](https://www.ruby-forum.com/t/validates-inclusion-of-on-boolean-within-file-fails/181518/3 "2010-01-13T21:48:02Z")

</div>

Matt J. wrote:

> Essentially, anything that isn’t blank and isn’t “true-y” (see the  
> list in the source) gets mapped to false.
> 
> About the only way I can think of to avoid this is to override the  
> accessors for those variables and map everything that isn’t true to  
> nil.
> 
> –Matt J.

Thanks Matt,

In the relative scheme of things it works in the way it should. If  
someone places a boolean value of true it is indeed true. If someone  
places a boolean value of false it is indeed false. If someone leaves  
it empty, it will validate incorrectly and the message will be  
displayed. So, overall, it’s working fine.

If, however, someone places some strange value that is not true or false  
it also returns false. I’m fine with that as it is highly unlikely that  
someone would do such a thing and if they do, it returns the expected  
behavior, only it does not send them the message.

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [January 14, 2010, 10:40am UTC](https://www.ruby-forum.com/t/validates-inclusion-of-on-boolean-within-file-fails/181518/4 "2010-01-14T10:40:24Z")

</div>

2010/1/13 Alpha B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Â layouts: true  
> Â swfs: true
> 
> I’m reading all of these yml values through a file upload which stores  
> it into a hash.
> 
> When saving the values to my database, all of the validators work  
> perfectly with the exception of the boolean reads.
> 
> validates\_inclusion\_of :stylesheets, :javascripts, :layouts, :swfs, :in  
> =\> [true, false]

Would it work if you used :in [“true”,“false”]? I have not tried it.

Colin

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [January 14, 2010, 3:09pm UTC](https://www.ruby-forum.com/t/validates-inclusion-of-on-boolean-within-file-fails/181518/5 "2010-01-14T15:09:16Z")

</div>

Colin L. wrote:

> 2010/1/13 Alpha B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> > Â layouts: true  
> > Â swfs: true
> > 
> > I’m reading all of these yml values through a file upload which stores  
> > it into a hash.
> > 
> > When saving the values to my database, all of the validators work  
> > perfectly with the exception of the boolean reads.
> > 
> > validates\_inclusion\_of :stylesheets, :javascripts, :layouts, :swfs, :in  
> > =\> [true, false]
> 
> Would it work if you used :in [“true”,“false”]? I have not tried it.
> 
> Colin

Yeah, I had tried that and also tried using %w with the same values,  
both of which caused all 4 validations on the boolean fields within the  
yaml file to fail. It probably would work if I put my yaml file to use  
something similar:

stylesheets: “true”  
javascripts: “false”  
etc.

It probably requires the implicit string reasoning for those values.

I’m going to keep it the way I have it right now though.

Thanks for the follow-up Colin, it’s always appreciated.
