# Store multiple check\_box of same attribute in "Search" class

**URL:** <https://www.ruby-forum.com/t/store-multiple-check-box-of-same-attribute-in-search-class/183184>\
**Category:** Rails\
**Created:** [February 9, 2010, 6:14pm UTC](https://www.ruby-forum.com/t/store-multiple-check-box-of-same-attribute-in-search-class/183184 "2010-02-09T18:14:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Learn\_by\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e274bd/32.png) [@Learn\_by\_D](https://www.ruby-forum.com/u/Learn_by_D)\
**Post date:** [February 9, 2010, 6:14pm UTC](https://www.ruby-forum.com/t/store-multiple-check-box-of-same-attribute-in-search-class/183184/1 "2010-02-09T18:14:16Z")

</div>

How do I store the value of multiple checkboxes as a result of using  
“check\_box” helper in a “Search” class? A “Search” class is explained  
here: [#111 Advanced Search Form - RailsCasts](http://railscasts.com/episodes/111-advanced-search-form). For  
example, a user should be able to search for all “Gay” OR “Bisexual”  
users by clicking both checkboxes. These are values of attribute  
“orientation”. My own research shows that I should do something like  
this:

Store Search[:orientations] in the database in “text”.  
In the search form:

\<% form\_for @search do |f| %\>  
\<%= f.check\_box orientations[], {}, 0, nil %\>Straight  
\<%= f.check\_box orientations[], {}, 1, nil %\>Gay  
\<%= f.check\_box orientations[], {}, 2, nil %\>Bisexual

Then in the Search model:  
serialize :orientations  
See [http://railsforum.com/viewtopic.php?id=15176](http://railsforum.com/viewtopic.php?id=15176)

Will this work? I have not implemented this but I would like to know  
if this is the right direction. I already see one problem: How does  
the search form remember the checkboxes that the user has checked?  
Unlike the radio\_button helper where if the current value of method is  
tag\_value the radio button will be checked, I don’t see where  
check\_box examines the current value of orientations[] from the  
database.

Thanks.

---

<div class="post-metadata">

**Author:** ![Learn\_by\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e274bd/32.png) [@Learn\_by\_D](https://www.ruby-forum.com/u/Learn_by_D)\
**Post date:** [February 13, 2010, 2:54am UTC](https://www.ruby-forum.com/t/store-multiple-check-box-of-same-attribute-in-search-class/183184/2 "2010-02-13T02:54:41Z")

</div>

Can some one help me with this question below? Thanks.

How do I store the value of multiple checkboxes as a result of using  
“check\_box” helper in a “Search” class? A “Search” class is explained  
here: [#111 Advanced Search Form - RailsCasts](http://railscasts.com/episodes/111-advanced-search-form). For  
example, a user should be able to search for all “Gay” OR “Bisexual”  
users by clicking both checkboxes. These are values of attribute  
“orientation”. My own research shows that I should do something like  
this:

Store Search[:orientations] in the database in “text”.  
In the search form:

\<% form\_for @search do |f| %\>  
\<%= f.check\_box orientations[], {}, 0, nil %\>Straight  
\<%= f.check\_box orientations[], {}, 1, nil %\>Gay  
\<%= f.check\_box orientations[], {}, 2, nil %\>Bisexual

Then in the Search model:  
serialize :orientations  
See [http://railsforum.com/viewtopic.php?id=15176](http://railsforum.com/viewtopic.php?id=15176)

Will this work? I have not implemented this but I would like to know  
if this is the right direction. I already see one problem: How does  
the search form remember the checkboxes that the user has checked?  
Unlike the radio\_button helper where if the current value of method is  
tag\_value the radio button will be checked, I don’t see where  
check\_box examines the current value of orientations[] from the  
database.

Thanks.
