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| %>
<td class="<%= @class -%>"><%= 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 %></td>
<% end %>
<td width="100%" align="center" class="<%=
@class -%>"><%round_counter + 1 %></td>
<% 3.times do |p| %>
<td class="<%= @class -%>"><%= 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 %></td>
<% end %>
<% end %>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
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 On Apr 10, 2:53 pm, edberner <eber...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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| %> > <td class="<%= @class -%>"><%= 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 %></td> > <% end %> > > <td width="100%" align="center" class="<%= @class -%>"><%> round_counter + 1 %></td> > > <% 3.times do |p| %> > <td class="<%= @class -%>"><%= 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 %></td> > <% end %> > <% end %>--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Does this make sense? On Apr 10, 3:19 pm, edberner <eber...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 > > On Apr 10, 2:53 pm, edberner <eber...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > 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| %> > > <td class="<%= @class -%>"><%= 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 %></td> > > <% end %> > > > <td width="100%" align="center" class="<%= @class -%>"><%> > round_counter + 1 %></td> > > > <% 3.times do |p| %> > > <td class="<%= @class -%>"><%= 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 %></td> > > <% end %> > > <% end %>--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Got it. needed to create more models. and write a crazy view On Apr 10, 2008, at 3:19 PM, edberner wrote:> > 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 > > On Apr 10, 2:53 pm, edberner <eber...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> 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| %> >> <td class="<%= @class -%>"><%= 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 %></td> >> <% end %> >> >> <td width="100%" align="center" class="<%= @class -%>"><%>> round_counter + 1 %></td> >> >> <% 3.times do |p| %> >> <td class="<%= @class -%>"><%= 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 %></td> >> <% end %> >> <% end %> > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ellis Berner wrote:> Got it. > needed to create more models. > and write a crazy viewup visit a rubyonrails website :http://www.rorchina.net wolf union program club :http://wolf.rorchina.net China Rubyonrails club: http://bbs.rorchina.net -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---