Using check_box with a list of objects

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

I have two models, players and player_stats


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.


Well, I changed the check_box() method in the view to the following:
<td><%= check_box("player[]", 'stats[1]') %></td>

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?


Starr wrote:

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.