Iterate over form data

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

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

Hi Fred,

your solution helped me a into the right direction! Thanks

Rune