I'm trying to assign players to a club. A club has n players and a player belongs to a club. A club can only have less than 23 players and no more than 2 players playing in the same position.
clubs = Club.all #Club is a datamapper object. Returns 20 clubs
to_generate = 10000
while (to_generate > 0)
p = Player.new #Player is a datamapper object
p.position = position #position is a random integer defined elsewhere
clubs.each do |club|
count = 0
club.players.each do |club_player|
if (club_player.position == p.position)
count += 1
end
end
if (count < 2 && club.players.length < 22)
club.players << p
p.club = club
end
end
p.save
to_generate -= 1
end
At the end of the script, I expect that all the clubs have 22 players. Why it's not like that?
EDIT: At the end of the script I only get 22 players assigned to the last club (20) and 10000 players generated