for some reason i can not get toss_won and toss_won to be saved properly. during testing i replaced toss_won = 34, toss_lost = 45 and everything worked but i have also tested the values generated by: toss_lost = check_teams[0][1], tested in console as well. @teams is an array of the two teams ie: [ [''rangers'' 23] [''red sox'' 45] ] if someone has an easier way or how to fix this let me know. what is being saved constantly to game.bat_1 and game. @game = Game.new(params[:game]) # select which teams batted and bowled first and seccond check_teams = @teams toss_won = params[:toss_won] #delete the team that won the toss check_teams.delete_if { |a| [*a].include?(toss_won) } #check_teams is now an array with 1 element consisting of name and id #toss_lost extracts the team id which is to be saved. toss_lost = check_teams[0][1] # check to see if the team that won the toss elected to ''Bat'' or ''Pitch'' if params[:elected] == "Bat" @game.bat_1 = toss_won @game.bat_2 = toss_lost @game.field_1 = toss_lost @game.field_2 = toss_won else @game.bat_1 = toss_lost @game.bat_2 = toss_won @game.field_1 = toss_won @game.field_2 = toss_lost 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 am having a problem with the if statement here. i know which team won the toss and what they elected to do. i have check the values/equations in the console and it works, but when the data is saved it always save only bat_1 and and field_2 one note: when i swapped toss_won and toss_lost with test numbers everything runs fine????? @game = Game.new(params[:game]) # select which teams batted and fielded first and second #make a copy of @team which is an array [["Rangers", 27], ["Red Sox", 29]] check_teams = @teams say toss_won = 29 as an example toss_won = params[:toss_won] #delete the team that won the toss, this removes the array set of the team that lost the toss check_teams.delete_if { |a| [*a].include?(toss_won) } #this gets the number from the array check_teams which is now like [["Rangers", 27]] toss_lost = check_teams[0][1] #check to see what the team the won the toss did, team 29 say elected to ''Bat'' then # put team numbers to bat first, bat second.. if params[:elected] == "Bat" @game.bat_1 = toss_won @game.bat_2 = toss_lost @game.field_1 = toss_lost @game.field_2 = toss_won else @game.bat_1 = toss_lost @game.bat_2 = toss_won @game.field_1 = toss_won @game.field_2 = toss_lost 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 -~----------~----~----~----~------~----~------~--~---
One possible source of confusion is the following: #make a copy of @team which is an array [["Rangers", 27], ["Red Sox", 29]] check_teams = @teams The comment is _not_ accurate. Essentially what you''ve done is create a new pointer to the same object, not create a copy of it. As a result, everything that you do to check_team is also done to @teams. I don''t know if that''s effecting the results you''re seeing but it''s worth being aware of. If you actually want a completely separate copy of the exact same structure, you''d need check_teams = @teams.dup Also, a note on your model. What is the purpose of having two sets of fields that are exactly the inverse of one another? That is, bat_1 is always field_2 and vice versa. That could become an expensive redundancy. You might want to do this as well: check_teams.delete_if { |a| [*a].include?(toss_won) } .flatten => ["Rangers", 27] Similarly, toss_lost = check_teams.delete_if { |a| [*a].include? (toss_won) } .flatten,last => 27 @game.bat_1 = params[:elected]=="Bat" ? toss_won : toss_lost @game.bat_2 = params[:elected]=="Bat" ? toss_lost : toss_won @game.field_1, @game.field_2 = @game.bat_2, @game.bat_1 On Apr 24, 9:00 am, rushnosh <rashan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> i am having a problem with the if statement here. > i know which team won the toss and what they elected to do. > > i have check the values/equations in the console and it works, but > when the data is saved it always save > only bat_1 and and field_2 > > one note: when i swapped toss_won and toss_lost with test numbers > everything runs fine????? > > @game = Game.new(params[:game]) > # select which teams batted and fielded first and second > > #make a copy of @team which is an array [["Rangers", 27], ["Red > Sox", 29]] > check_teams = @teams > > say toss_won = 29 as an example > toss_won = params[:toss_won] > > #delete the team that won the toss, this removes the array set of > the team that lost the toss > check_teams.delete_if { |a| [*a].include?(toss_won) } > > #this gets the number from the array check_teams which is now like > [["Rangers", 27]] > toss_lost = check_teams[0][1] > > #check to see what the team the won the toss did, team 29 say > elected to ''Bat'' then > # put team numbers to bat first, bat second.. > if params[:elected] == "Bat" > @game.bat_1 = toss_won > @game.bat_2 = toss_lost > @game.field_1 = toss_lost > @game.field_2 = toss_won > else > @game.bat_1 = toss_lost > @game.bat_2 = toss_won > @game.field_1 = toss_won > @game.field_2 = toss_lost > 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 -~----------~----~----~----~------~----~------~--~---
andy thanks for taking the time. i did solve this and i will make my notes below because there is still some confusion. i am glad that i was able to figure out the problem. On Apr 24, 10:24 am, AndyV <AndyVana...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> One possible source of confusion is the following: > #make a copy of @team which is an array [["Rangers", 27], ["Red > Sox", 29]] > check_teams = @teams > > The comment is _not_ accurate. Essentially what you''ve done is create > a new pointer to the same object, not create a copy of it.yes, i realize that, that was put in while i was testing the code, it''s not necessary.> Also, a note on your model. What is the purpose of having two sets of > fields that are exactly the inverse of one another? That is, bat_1 is > always field_2 and vice versa. That could become an expensive > redundancy.thanks for pointing this out. i agree.> > You might want to do this as well:the previous logic was working, the problem was with toss_won = params[:toss_won] for some reason this didn''t work, but when i changed it to. toss_won = @game.toss_won it worked @game was simply @game = Game.new(params[:game])> > check_teams.delete_if { |a| [*a].include?(toss_won) } .flatten > => ["Rangers", 27] >i really like the code below, thanks.> @game.bat_1 = params[:elected]=="Bat" ? toss_won : toss_lost > @game.bat_2 = params[:elected]=="Bat" ? toss_lost : toss_won > @game.field_1, @game.field_2 = @game.bat_2, @game.bat_1 > > On Apr 24, 9:00 am,rushnosh<rashan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > i am having a problem with the if statement here. > > i know which team won the toss and what they elected to do. > > > i have check the values/equations in the console and it works, but > > when the data is saved it always save > > only bat_1 and and field_2 > > > one note: when i swapped toss_won and toss_lost with test numbers > > everything runs fine????? > > > @game = Game.new(params[:game]) > > # select which teams batted and fielded first and second > > > #make a copy of @team which is an array [["Rangers", 27], ["Red > > Sox", 29]] > > check_teams = @teams > > > say toss_won = 29 as an example > > toss_won = params[:toss_won] > > > #delete the team that won the toss, this removes the array set of > > the team that lost the toss > > check_teams.delete_if { |a| [*a].include?(toss_won) } > > > #this gets the number from the array check_teams which is now like > > [["Rangers", 27]] > > toss_lost = check_teams[0][1] > > > #check to see what the team the won the toss did, team 29 say > > elected to ''Bat'' then > > # put team numbers to bat first, bat second.. > > if params[:elected] == "Bat" > > @game.bat_1 = toss_won > > @game.bat_2 = toss_lost > > @game.field_1 = toss_lost > > @game.field_2 = toss_won > > else > > @game.bat_1 = toss_lost > > @game.bat_2 = toss_won > > @game.field_1 = toss_won > > @game.field_2 = toss_lost > > 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 -~----------~----~----~----~------~----~------~--~---