# Only one of six

**URL:** <https://www.ruby-forum.com/t/only-one-of-six/134438>\
**Category:** Rails\
**Created:** [April 10, 2008, 8:54pm UTC](https://www.ruby-forum.com/t/only-one-of-six/134438 "2008-04-10T20:54:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![edberner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f9ae1b/32.png) [@edberner](https://www.ruby-forum.com/u/edberner)\
**Post date:** [April 10, 2008, 8:54pm UTC](https://www.ruby-forum.com/t/only-one-of-six/134438/1 "2008-04-10T20:54:24Z")

</div>

Why does my database only contain entries for the first player? It  
successfully saves all rounds for the first player but not for 2-6.  
Any ideas?

Game Controller  
def create  
@game = Game.new(params[:game])

```
if @game.save
  @opponent.games << @game
  5.times { @game.rounds.create }

```

…  
end

def update  
params[:game][:existing\_round\_attributes] ||= {}  
@game = Game.find(params[:id])

```
if @game.update_attributes(params[:game])

```

…  
end

Game Model  
def new\_round\_attributes=(round\_attributes)  
round\_attributes.each do |attributes|  
rounds.build(attributes)  
end  
end

def existing\_round\_attributes=(round\_attributes)  
rounds.reject(&:new\_record?).each do |round|  
attributes = round\_attributes[round.id.to\_s]  
if attributes  
round.attributes = attributes  
else  
rounds.delete(round)  
end  
end  
end

Game Helper  
def fields\_for\_round(round, &block)  
prefix = round.new\_record? ? ‘new’ : ‘existing’  
fields\_for(“game[#{prefix}\_round\_attributes][]”, round, &block)  
end

\<% fields\_for\_round(round) do |f| %\>  
\<% @class = cycle("", “alt”, :name =\> “colors”) -%\>  
\<% 3.times do |p| %\>

\<%= f.select(:action, [“Miss”, “Skip”,  
“1”, “2”, “3”, “4”, “5”, “6”, “7”, “8”, “9”, “10”], :index =\> nil) %\>  
\<%= f.hidden\_field :player\_id, :value =\>  
@game.teams[0].players[p].id %\>  
\<% end %\> \<%= round\_counter + 1 %\>

\<% 3.times do |p| %\>

\<%= f.select(:action, [“Miss”, “Skip”,  
“1”, “2”, “3”, “4”, “5”, “6”, “7”, “8”, “9”, “10”], :index =\> nil) %\>  
\<%= f.hidden\_field :player\_id, :value =\>  
@game.teams[1].players[p].id %\>  
\<% end %\>  
\<% end %\>

---

<div class="post-metadata">

**Author:** ![edberner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f9ae1b/32.png) [@edberner](https://www.ruby-forum.com/u/edberner)\
**Post date:** [April 10, 2008, 9:20pm UTC](https://www.ruby-forum.com/t/only-one-of-six/134438/2 "2008-04-10T21:20:53Z")

</div>

I’m sorry if this is not clear enough. Just let me know if you need  
more explanation.

My database contains entries  
id = 1…5  
game\_id = 1  
player\_id = 1  
action = whatever they choose

i want it to look like  
id = 1…30  
game\_id = 1  
player\_id = 1…3  
action = whatever they choose

---

<div class="post-metadata">

**Author:** ![edberner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f9ae1b/32.png) [@edberner](https://www.ruby-forum.com/u/edberner)\
**Post date:** [April 10, 2008, 10:36pm UTC](https://www.ruby-forum.com/t/only-one-of-six/134438/3 "2008-04-10T22:36:50Z")

</div>

Does this make sense?

---

<div class="post-metadata">

**Author:** ![edberner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f9ae1b/32.png) [@edberner](https://www.ruby-forum.com/u/edberner)\
**Post date:** [April 11, 2008, 1:20am UTC](https://www.ruby-forum.com/t/only-one-of-six/134438/4 "2008-04-11T01:20:56Z")

</div>

Got it.  
needed to create more models.  
and write a crazy view

---

<div class="post-metadata">

**Author:** ![edberner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f9ae1b/32.png) [@edberner](https://www.ruby-forum.com/u/edberner)\
**Post date:** [April 11, 2008, 5:47am UTC](https://www.ruby-forum.com/t/only-one-of-six/134438/5 "2008-04-11T05:47:18Z")

</div>

Ellis B. wrote:

> Got it.  
> needed to create more models.  
> and write a crazy view

up

visit a rubyonrails website :[http://www.rorchina.net](http://www.rorchina.net)  
wolf union program club :[http://wolf.rorchina.net](http://wolf.rorchina.net)  
China Rubyonrails club: [http://bbs.rorchina.net](http://bbs.rorchina.net)
