# Updating the checkboxes with data from the db

**URL:** <https://www.ruby-forum.com/t/updating-the-checkboxes-with-data-from-the-db/128631>\
**Category:** Rails\
**Created:** [February 9, 2008, 4:39pm UTC](https://www.ruby-forum.com/t/updating-the-checkboxes-with-data-from-the-db/128631 "2008-02-09T16:39:57Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![dezer85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/57b2e6/32.png) [@dezer85](https://www.ruby-forum.com/u/dezer85)\
**Post date:** [February 9, 2008, 4:39pm UTC](https://www.ruby-forum.com/t/updating-the-checkboxes-with-data-from-the-db/128631/1 "2008-02-09T16:39:57Z")

</div>

I’m making a musicians site where the users are able to check some  
checkboxes with their music-style if they want to edit their music-style  
i would like the previous checked checkboxes to be checked.

here’s some code

 \<%= label %\>: \<% @i = 0 %\> \<% @status = false%\> \<% collection.each do |db\_field| %\> \<% db\_collection.split(", ").each do |collection\_field| %\> \<% if db\_field == collection\_field %\> \<% @status = true %\> \<% else %\> \<% @status = false%\> \<% end %\> \<% end %\> \<% if @i == 1 %\> \<% @i = -1 %\> \<% end %\> \<% @i = @i +1 %\> \<% end %\>

| \<%= check\_box\_tag array, db\_field, @status %\> \<%= db\_field %\> |
|
| |

collection could be:

VALID\_STYLES = [“Blues”, “Country”, “Elektronisk”, “Folkemusik”,  
“Hip-Hop”,  
“Jazz”, “Klassisk”, “Metal”, “Pop”, “R&B og Soul”,  
“Rock”,  
“Verdensmusik”]

and the db\_collection could be:

Blues, Elektronisk, R&B og Soul

I don’t know why, but with the code i have now R&B og Soul would get  
updated in the view so that the checkbox would be checked, it’s always  
the last option that gets checked.
