# Iterate over form data

**URL:** <https://www.ruby-forum.com/t/iterate-over-form-data/145455>\
**Category:** Rails\
**Created:** [August 9, 2008, 10:16pm UTC](https://www.ruby-forum.com/t/iterate-over-form-data/145455 "2008-08-09T22:16:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rsoerensen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/58956e/32.png) [@rsoerensen](https://www.ruby-forum.com/u/rsoerensen)\
**Post date:** [August 9, 2008, 10:16pm UTC](https://www.ruby-forum.com/t/iterate-over-form-data/145455/1 "2008-08-09T22:16:06Z")

</div>

Hi,

I’m building a system for registrering table soccer matches. I have  
however run into this problem:

Each time a match is registered, a match is created in a match table. A  
match has\_many participants, and the MatchParticipant table contains  
these informations: user\_id, match\_id, side (a or b), score and winner  
(true/false). It’s the latter I’m having trouble with. So a match might  
have for instance 2 matchparticipants associated with it.

So, I wan’t to iterate over the data that the user sends from a form to  
decide which participant is the winner. Basically it’s just comparing  
the score of participant #1 to the score of participant #2 and then  
return true or false to be writtin in the Matchparticipant winner  
column. But how do I do this?

The parameters sent from the user could look like this:

{ “commit”=\>“Send”,  
“authenticity\_token”=\>“8f2be904e8e5850e882ccc74090fd79ef40b88bf”,  
“match”=\>{“participant\_attributes”=\>[{“score”=\>“10”,  
“side”=\>“a”,  
“player\_id”=\>“1”},  
{“score”=\>“5”,  
“side”=\>“b”,  
“player\_id”=\>“2”}]}}

So I just want to add for instance: “winner” =\> “true”/“false”, to each  
participant when I build the row, depending on whether they are the  
winner or not.

If you need more information I’ll be happy to provide it to you.

Thanks 🙂

---

<div class="post-metadata">

**Author:** ![rsoerensen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/58956e/32.png) [@rsoerensen](https://www.ruby-forum.com/u/rsoerensen)\
**Post date:** [August 9, 2008, 11:53pm UTC](https://www.ruby-forum.com/t/iterate-over-form-data/145455/2 "2008-08-09T23:53:46Z")

</div>

On 9 Aug 2008, at 21:16, Rune Soerensen wrote:

> “player\_id”=\>“2”}]}}
> 
> So I just want to add for instance: “winner” =\> “true”/“false”, to  
> each  
> participant when I build the row, depending on whether they are the  
> winner or not.

Well you could have some like  
m = Match.new

winning\_participant = nil  
params[:match][:participant\_attributes].each do |, attributes|  
participant = m.participants.build attributes  
winning\_participant ||= participant  
winning\_participant = participant if participant.score \>  
winning\_participant.score  
end  
winning\_participant.winner = true  
m.save

which is a fancy way of saying

Fred

---

<div class="post-metadata">

**Author:** ![rsoerensen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/58956e/32.png) [@rsoerensen](https://www.ruby-forum.com/u/rsoerensen)\
**Post date:** [August 11, 2008, 11:22pm UTC](https://www.ruby-forum.com/t/iterate-over-form-data/145455/3 "2008-08-11T23:22:58Z")

</div>

Hi Fred,

your solution helped me a into the right direction! Thanks

Rune
