Iterate over form data


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",

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 :slight_smile:

Well you could have some like
m =

winning_participant = nil
params[:match][:participant_attributes].each do |, attributes|
   participant = attributes
   winning_participant ||= participant
   winning_participant = participant if participant.score > winning_participant.score
winning_participant.winner = true

which is a fancy way of saying


Hi Fred,

your solution helped me a into the right direction! Thanks