# Typecasting and boolean attributes

**URL:** <https://www.ruby-forum.com/t/typecasting-and-boolean-attributes/52224>\
**Category:** Rails\
**Created:** [January 12, 2006, 2:45am UTC](https://www.ruby-forum.com/t/typecasting-and-boolean-attributes/52224 "2006-01-12T02:45:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![derek054](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b19c9b/32.png) [@derek054](https://www.ruby-forum.com/u/derek054)\
**Post date:** [January 12, 2006, 2:45am UTC](https://www.ruby-forum.com/t/typecasting-and-boolean-attributes/52224/1 "2006-01-12T02:45:55Z")

</div>

I have 2 radio buttons like this:

\<%= radio\_button ‘group’, ‘public’, true %\>  
\<%= radio\_button ‘group’, ‘public’, false %\>

They hold the correct values when viewing the @group object. However,  
when updating, it does not appear that the params[:group][:public]  
value is being typecast correctly.

As params[:group][:public] = “true”, “true” should be resolved to a  
“1” in the database. However, this is not occurring.

Has anyone else seen this issue or have a suggested fix?

## Derek

Derek H.  
HighGroove Studios - [http://www.highgroove.com](http://www.highgroove.com)  
Atlanta, GA | San Mateo, CA  
Keeping it Simple.  
404.593.4879

---

<div class="post-metadata">

**Author:** ![derek054](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b19c9b/32.png) [@derek054](https://www.ruby-forum.com/u/derek054)\
**Post date:** [January 12, 2006, 3:20am UTC](https://www.ruby-forum.com/t/typecasting-and-boolean-attributes/52224/2 "2006-01-12T03:20:31Z")

</div>

Derek H. wrote:

> when updating, it does not appear that the params[:group][:public]  
> value is being typecast correctly.
> 
> As params[:group][:public] = “true”, “true” should be resolved to a  
> “1” in the database. However, this is not occurring.

What is typecasting?

Seriously, radio buttons return string literals back in the params, so  
true comes back as “true”. If you want it to map to 1 or 0, set the  
radio button to return “1” or “0”.

in ruby at least…

true.to\_s =\> “true”

(true == 1) =\> false

\_Kevin

---

<div class="post-metadata">

**Author:** ![derek054](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b19c9b/32.png) [@derek054](https://www.ruby-forum.com/u/derek054)\
**Post date:** [January 12, 2006, 6:05am UTC](https://www.ruby-forum.com/t/typecasting-and-boolean-attributes/52224/3 "2006-01-12T06:05:11Z")

</div>

But that’s the problem…if I set the value of the radio button to  
“1”, it won’t show up as selected when rendering the view because  
Rails will only mark it selected if its value is “true.”

So if I set the value as 1, the attribute value is saved correctly to  
the DB…but then the button won’t be selected as the attribute it is  
tied to has a value of “true.”

As you said:  
true == 1.

- Derek

- Derek

On 1/11/06, Kevin O. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## true comes back as “true”. If you want it to map to 1 or 0, set the
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
Derek H.  
HighGroove Studios - [http://www.highgroove.com](http://www.highgroove.com)  
Atlanta, GA | San Mateo, CA  
Keeping it Simple.  
404.593.4879

---

<div class="post-metadata">

**Author:** ![derek054](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b19c9b/32.png) [@derek054](https://www.ruby-forum.com/u/derek054)\
**Post date:** [January 12, 2006, 7:05am UTC](https://www.ruby-forum.com/t/typecasting-and-boolean-attributes/52224/4 "2006-01-12T07:05:31Z")

</div>

\<%= radio\_button ‘group’, ‘public’, “1” %\>  
\<%= radio\_button ‘group’, ‘public’, “0” %\>

This works…

the params hash will look like this {… “group” =\> {“public”=\>“1”}}  
when the first one is selected.

The first radio button will be selected if …

group.public == “1”

Hope that helps…

\_Kevin

---

<div class="post-metadata">

**Author:** ![derek054](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b19c9b/32.png) [@derek054](https://www.ruby-forum.com/u/derek054)\
**Post date:** [January 12, 2006, 9:13am UTC](https://www.ruby-forum.com/t/typecasting-and-boolean-attributes/52224/5 "2006-01-12T09:13:16Z")

</div>

Yeah…I’ll give that another shot…making I was tired and just missed  
it.

Thanks for your help Kevin,

Derek

On 1/11/06, Kevin O. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> group.public == “1”  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
Derek H.  
HighGroove Studios - [http://www.highgroove.com](http://www.highgroove.com)  
Atlanta, GA | San Mateo, CA  
Keeping it Simple.  
404.593.4879
