# Using check\_box with a list of objects

**URL:** <https://www.ruby-forum.com/t/using-check-box-with-a-list-of-objects/74577>\
**Category:** Rails\
**Created:** [November 7, 2006, 10:48am UTC](https://www.ruby-forum.com/t/using-check-box-with-a-list-of-objects/74577 "2006-11-07T10:48:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ewildgoose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/bcef8e/32.png) [@ewildgoose](https://www.ruby-forum.com/u/ewildgoose)\
**Post date:** [November 7, 2006, 10:48am UTC](https://www.ruby-forum.com/t/using-check-box-with-a-list-of-objects/74577/1 "2006-11-07T10:48:46Z")

</div>

I am having trouble figuring out how to use a check box with a list of  
objects.

I have two models, players and player\_stats

# players

id  
name

# player\_stats

id  
player\_id  
game\_id  
goals  
assists

I want to present a list of players, and use a check box to determine  
if a player\_stat should be created for that player. In my view I’m  
trying to do something like this.

\<% for @player in @players %\>

 \<%= check\_box( "player[]", ... ) %\> \<%= @player.name %\> \<% end %\>

I don’t know what to use as the method in the check\_box() call. The  
check box doesn’t specify an attribute in either model, I want it to  
signify that a player\_stat should be created.

Is there a better way to approach this? I want to show the user a list  
of all players. The user will then select the players that played in a  
game, and a row in player\_stats will be created.

Thanks

---

<div class="post-metadata">

**Author:** ![ewildgoose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/bcef8e/32.png) [@ewildgoose](https://www.ruby-forum.com/u/ewildgoose)\
**Post date:** [November 7, 2006, 10:49am UTC](https://www.ruby-forum.com/t/using-check-box-with-a-list-of-objects/74577/2 "2006-11-07T10:49:18Z")

</div>

That looks like an ok approach. You set the checkbox name to something  
like stats[1], stats[2]. Then in the controller just use  
params[:stats][‘1’]. That way it’s independent of your player record.  
Another way is to include the stats field in the player record and  
remove it before passing params to the model.

Starr

---

<div class="post-metadata">

**Author:** ![ewildgoose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/bcef8e/32.png) [@ewildgoose](https://www.ruby-forum.com/u/ewildgoose)\
**Post date:** [November 7, 2006, 10:50am UTC](https://www.ruby-forum.com/t/using-check-box-with-a-list-of-objects/74577/3 "2006-11-07T10:50:23Z")

</div>

Well, I changed the check\_box() method in the view to the following:

\<%= check\_box("player[]", 'stats[1]') %\>

I now get this error.  
undefined method `stats[1]’ for #Player:0x41a5c0e4

Here is what the api doc says check\_box is expecting.  
“Returns a checkbox tag tailored for accessing a specified attribute  
(identified by method) on an object assigned to the template  
(identified by object). It’s intended that method returns an integer  
and if that integer is above zero, then the checkbox is checked.”

So, “stats” is not an attribute of Player, and that is why I’m getting  
the error.

Each row in player\_stats tells me that a player played in a game. Will  
adding a “played” column to player\_stats solve the problem?

Thanks

---

<div class="post-metadata">

**Author:** ![ewildgoose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/bcef8e/32.png) [@ewildgoose](https://www.ruby-forum.com/u/ewildgoose)\
**Post date:** [November 7, 2006, 10:50am UTC](https://www.ruby-forum.com/t/using-check-box-with-a-list-of-objects/74577/4 "2006-11-07T10:50:40Z")

</div>

OK, here’s the solution I’ve come up with.

1. I added a column to player\_stats named ‘played’.

2. When getting my list of players, I left join player\_stats so that I  
get a list of players even if no row in player\_stats exists.

3. I use ‘played’ as the method in my call to check\_box().

The ‘played’ column seems a bit redundant, if the row exists in  
player\_stats, played will always be 1. I couldn’t think of another  
solution, though.

Here is what the view code looks like and the corresponding html.

# View
\<%= check\_box("visiting\_player[]", 'played') %\> \<%= @visiting\_player.person.last\_name %\>, \<%= @visiting\_player.person.first\_name %\>
# HTML
Jones, Matt
