# Check\_box problem

**URL:** <https://www.ruby-forum.com/t/check-box-problem/132835>\
**Category:** Rails\
**Created:** [March 25, 2008, 6:27am UTC](https://www.ruby-forum.com/t/check-box-problem/132835 "2008-03-25T06:27:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![MrSBless](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3bc359/32.png) [@MrSBless](https://www.ruby-forum.com/u/MrSBless)\
**Post date:** [March 25, 2008, 6:27am UTC](https://www.ruby-forum.com/t/check-box-problem/132835/1 "2008-03-25T06:27:42Z")

</div>

I have used check\_box\_tag method to store the select value in the  
string data type attribute in database with multiple selection  
seperated by comma. My problem is that when I need to edit that  
object(row) how can I make the check\_box checked for the value in that  
string data type attribute of my database.

---

<div class="post-metadata">

**Author:** ![MrSBless](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3bc359/32.png) [@MrSBless](https://www.ruby-forum.com/u/MrSBless)\
**Post date:** [March 25, 2008, 7:35am UTC](https://www.ruby-forum.com/t/check-box-problem/132835/2 "2008-03-25T07:35:53Z")

</div>

Mr. Bless wrote:

> I have used check\_box\_tag method to store the select value in the  
> string data type attribute in database with multiple selection  
> seperated by comma. My problem is that when I need to edit that  
> object(row) how can I make the check\_box checked for the value in that  
> string data type attribute of my database.

maybe pass it :selected =\> true ?

---

<div class="post-metadata">

**Author:** ![MrSBless](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3bc359/32.png) [@MrSBless](https://www.ruby-forum.com/u/MrSBless)\
**Post date:** [March 25, 2008, 5:57pm UTC](https://www.ruby-forum.com/t/check-box-problem/132835/3 "2008-03-25T17:57:11Z")

</div>

The third argument that you pass to check\_box\_tag is either true or  
false, and if it’s true, the box is checked. I’m using it like this:

\<%= check\_box\_tag s.id, ‘1’, (params[:selected\_services] &&  
params[:selected\_services].include?(s.id.to\_s)) %\>

The third argument looks to see if there’s a  
params[:selected\_services] value and then checks to see whether or not  
the current value is included in the params (which is ugly, but it  
works so far). You might be able to modify my example and generate an  
array from your database values (object.atr.split(‘,’) or something),  
passing it to include.

…or something like that.

On Mar 25, 1:35 am, Roger P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![MrSBless](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3bc359/32.png) [@MrSBless](https://www.ruby-forum.com/u/MrSBless)\
**Post date:** [March 26, 2008, 4:08pm UTC](https://www.ruby-forum.com/t/check-box-problem/132835/4 "2008-03-26T16:08:33Z")

</div>

You can always work on efficiency later… 🙂 Honestly, I’m not good  
at writing efficient code yet, so I tend to find something that works  
first and then worry about efficiency. If you’re not dealing with  
immense amounts of data, then a few thousandths of a second here and  
there usually aren’t worth fretting over until you’ve covered other  
more important aspects of your app.

It doesn’t seem like storing a CSV in a database is terribly efficient  
to begin with - it almost makes you iterate over the field to get the  
values. Perhaps using a separate table and an AR association would  
work better, but I’m not sure what you’re working with.

-Kyle

---

<div class="post-metadata">

**Author:** ![MrSBless](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3bc359/32.png) [@MrSBless](https://www.ruby-forum.com/u/MrSBless)\
**Post date:** [March 26, 2008, 7:31am UTC](https://www.ruby-forum.com/t/check-box-problem/132835/5 "2008-03-26T07:31:26Z")

</div>

Thanks a lot. But I still got one more question. Since I have stored  
the data in comma separated values in database field. Now while  
editing I have to show the checked only for those items that were  
saved in the database. Writing each and every condition using if else  
is not what I am looking for. I want something that is efficient and  
less code. Plz… Help
