# cerating multiple cjeck boxes using array

**URL:** <https://discuss.rubyonrails.org/t/cerating-multiple-cjeck-boxes-using-array/56475>\
**Category:** rubyonrails-talk\
**Created:** [December 28, 2010, 2:57am UTC](https://discuss.rubyonrails.org/t/cerating-multiple-cjeck-boxes-using-array/56475 "2010-12-28T02:57:15Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [December 28, 2010, 2:57am UTC](https://discuss.rubyonrails.org/t/cerating-multiple-cjeck-boxes-using-array/56475/1 "2010-12-28T02:57:15Z")

</div>

i have a registration form ,i want to store data in corresponding user table in that hobbies field ,i want to store all hobbies in one column so i take multiple check boxes and store in one array all checked values for that i am using below tag,but not working,please any suggestion \<%= check\_box(:user ,:hobbies,:value =\>'Games') %\>Games &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<%= check\_box :user , :hobbies,:value =\>'Music' %\>Music &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<%= check\_box :user , :hobbies,:value =\> 'Shopping'%\>Shopping the above user is table name ,hobbies is column name

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [December 28, 2010, 9:27am UTC](https://discuss.rubyonrails.org/t/cerating-multiple-cjeck-boxes-using-array/56475/2 "2010-12-28T09:27:16Z")

</div>

> i have a registration form ,i want to store data in corresponding user table in that hobbies field ,i want to store all hobbies in one column so i take multiple check boxes and store in one array all checked values for that i am using below tag,but not working,please any suggestion \<%= check\_box(:user ,:hobbies,:value =\>'Games') %\>Games \<%= check\_box :user , :hobbies,:value =\>'Music' %\>Music \<%= check\_box :user , :hobbies,:value =\> 'Shopping'%\>Shopping the above user is table name ,hobbies is column name

You need to use check\_box\_tag, and the name of the parameter needs to end in . THis will result in params[:foo] being an array which you an then do something sensible with.

Fred

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [December 28, 2010, 11:49am UTC](https://discuss.rubyonrails.org/t/cerating-multiple-cjeck-boxes-using-array/56475/3 "2010-12-28T11:49:42Z")

</div>

thank you ,thank you fred ,it will help in mytask

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [December 28, 2010, 1:56pm UTC](https://discuss.rubyonrails.org/t/cerating-multiple-cjeck-boxes-using-array/56475/4 "2010-12-28T13:56:20Z")

</div>

Prasad B. wrote in post #970999:

> i have a registration form ,i want to store data in corresponding user table in that hobbies field ,i want to store all hobbies in one column

No you don't. That's VERY BAD database design. Please read about DB normalization.

Best,

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [December 30, 2010, 7:17am UTC](https://discuss.rubyonrails.org/t/cerating-multiple-cjeck-boxes-using-array/56475/5 "2010-12-30T07:17:13Z")

</div>

Frederick Cheung wrote in post #971033:

> > 
> 
> You need to use check\_box\_tag, and the name of the parameter needs to end in . THis will result in params[:foo] being an array which you an then do something sensible with.
> 
> Fred

Thank you so much fred for your suggestion ,it will create for user registration page,please give me a solution for in user profile editing how it works .example i have 3 check boxes in editing ,which one is select in previous they are automatically checked please give any solution

Thanks & Regards prasad

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [December 30, 2010, 1:39pm UTC](https://discuss.rubyonrails.org/t/cerating-multiple-cjeck-boxes-using-array/56475/6 "2010-12-30T13:39:03Z")

</div>

please it is urgent for me any suggestion urgent

---

<div class="post-metadata">

**Author:** ![Colin\_Law1](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@Colin\_Law1](https://discuss.rubyonrails.org/u/Colin_Law1)\
**Post date:** [December 30, 2010, 1:46pm UTC](https://discuss.rubyonrails.org/t/cerating-multiple-cjeck-boxes-using-array/56475/7 "2010-12-30T13:46:33Z")

</div>

Since you have not quoted any previous message it is not clear what you are wanting help with. Looking back at your original post it is not clear exactly what you are having difficulty with. I assume you have followed Marnen's advice in his email. So having done that can you explain what the problem is?

Colin

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [December 30, 2010, 1:51pm UTC](https://discuss.rubyonrails.org/t/cerating-multiple-cjeck-boxes-using-array/56475/8 "2010-12-30T13:51:06Z")

</div>

i have a registration form ,i want to store data in corresponding user table in that hobbies field ,i want to store all hobbies in one column so i take multiple check boxes and store in one array all checked values for that i am using below tag,but not working,please any suggestion \<%= check\_box(:user ,:hobbies,:value =\>'Games') %\>Games &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<%= check\_box :user , :hobbies,:value =\>'Music' %\>Music &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<%= check\_box :user , :hobbies,:value =\> 'Shopping'%\>Shopping the above user is table name ,hobbies is column name

the above check boxes ,i want in registration,validating and mainly editing i want how to possible

---

<div class="post-metadata">

**Author:** ![Colin\_Law1](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@Colin\_Law1](https://discuss.rubyonrails.org/u/Colin_Law1)\
**Post date:** [December 30, 2010, 2:04pm UTC](https://discuss.rubyonrails.org/t/cerating-multiple-cjeck-boxes-using-array/56475/9 "2010-12-30T14:04:30Z")

</div>

I am assuming that this is in response to my post, but again you have not quoted the previous message so I do not know. When replying please insert your reply into the previous message so that it is easier to follow the thread. Thanks

> i have a registration form ,i want to store data in corresponding user table in that hobbies field ,i want to store all hobbies in one column so i take multiple check boxes and store in one array all checked values for that i am using below tag,but not working,please any suggestion \<%= check\_box(:user ,:hobbies,:value =\>'Games') %\>Games \<%= check\_box :user , :hobbies,:value =\>'Music' %\>Music \<%= check\_box :user , :hobbies,:value =\> 'Shopping'%\>Shopping the above user is table name ,hobbies is column name

It has already been suggested that it is not a good idea to store the hobbies all in one column. What did you learn from reading about database normalisation? Hint: You could have a hobbies table and then have a has\_and\_belongs\_to\_many association between the models. Have a look at the Rails Guide on ActiveRecord Associations if this is not familiar to you.

Colin

---

<div class="post-metadata">

**Author:** ![Michael\_Pavling](https://avatars.discourse-cdn.com/v4/letter/m/8dc957/32.png) [@Michael\_Pavling](https://discuss.rubyonrails.org/u/Michael_Pavling)\
**Post date:** [December 30, 2010, 3:18pm UTC](https://discuss.rubyonrails.org/t/cerating-multiple-cjeck-boxes-using-array/56475/10 "2010-12-30T15:18:24Z")

</div>

> i have a registration form ,i want to store data in corresponding user table in that hobbies field ,i want to store all hobbies in one column so i take multiple check boxes and store in one array

At the risk of making the the thread start sounding like a broken record : "no you do not".

It's a dreadful idea to try to store all the hobbies in one (comma separated, I assume) field. Set up an association properly, and have done with it.

> all checked values for that i am using below tag,but not working,please any suggestion
> 
> the above check boxes ,i want in registration,validating and mainly editing i want how to possible

If you insist on implementing your horribly broken solution to the problem, then you'll need some helper methods in your model that take your single column and break it into an array, and take an array of ids (from the params array in the controller) and join it back into your field, such as:

&nbsp;&nbsp;def hobby\_ids &nbsp;&nbsp;&nbsp;&nbsp;self.hobbies.split(",") &nbsp;&nbsp;end

&nbsp;&nbsp;def hobby\_ids=(supplied\_ids) &nbsp;&nbsp;&nbsp;&nbsp;self.hobbies = supplied\_ids.join(",") &nbsp;&nbsp;end

No-one is going to "help" more than this, as the correct solution is to "make an association", anything else is a hideous bodge that is going to lead to heartache and worry.

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [December 31, 2010, 2:52am UTC](https://discuss.rubyonrails.org/t/cerating-multiple-cjeck-boxes-using-array/56475/11 "2010-12-31T02:52:46Z")

</div>

Hi thank you very much ,you got my point and understand my thinking you suggest good idea ,i am new to ruby on rails so please suggest rails tags also for array of check boxes.How serialize array of that hobbies object and store in database

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [December 31, 2010, 3:06am UTC](https://discuss.rubyonrails.org/t/cerating-multiple-cjeck-boxes-using-array/56475/12 "2010-12-31T03:06:25Z")

</div>

What is Mass Assignment? Can you give me a clear idea about that with an example?

---

<div class="post-metadata">

**Author:** ![Colin\_Law1](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@Colin\_Law1](https://discuss.rubyonrails.org/u/Colin_Law1)\
**Post date:** [December 31, 2010, 8:27am UTC](https://discuss.rubyonrails.org/t/cerating-multiple-cjeck-boxes-using-array/56475/13 "2010-12-31T08:27:54Z")

</div>

In order to understand better the basics of RoR I suggest you work through some tutorials and guides. Start with working through all the Rails Guides and also railstutorial.org (which is a free tutorial when used online). Then you will be able to answer most of your own questions. Come back and ask again if you still have problems.

Colin

---

<div class="post-metadata">

**Author:** ![Michael\_Pavling](https://avatars.discourse-cdn.com/v4/letter/m/8dc957/32.png) [@Michael\_Pavling](https://discuss.rubyonrails.org/u/Michael_Pavling)\
**Post date:** [December 31, 2010, 9:26am UTC](https://discuss.rubyonrails.org/t/cerating-multiple-cjeck-boxes-using-array/56475/14 "2010-12-31T09:26:01Z")

</div>

> Hi thank you very much ,you got my point and understand my thinking you suggest good idea

I've no idea who you're thanking, as you didn't quote any previous message.

> ,i am new to ruby on rails so please suggest rails tags also for array of check boxes.

googling for "ruby rails tutorial" will give you lots of results. I recall that you've already been pointed at the documentation for associations, and it might be good for you to get a book to work through - "Agile Web Development With Rails" is highly recommended.

> How serialize array of that hobbies object and store in database

You can use the "serialize" method on a model.... but that's \*not\* what you want to solve your problem - you want to use associations... it may have been mentioned already.

Sorry for sounding terse... got a plane to catch 🙂

---

<div class="post-metadata">

**Author:** ![soldier](https://avatars.discourse-cdn.com/v4/letter/s/3bc359/32.png) [@soldier](https://discuss.rubyonrails.org/u/soldier)\
**Post date:** [December 31, 2010, 10:13am UTC](https://discuss.rubyonrails.org/t/cerating-multiple-cjeck-boxes-using-array/56475/15 "2010-12-31T10:13:20Z")

</div>

you can use attr\_accessible.
