나는 선수를 클럽에 할당하려고합니다. 클럽에는 n 명의 플레이어가 있고 플레이어는 클럽에 속해 있습니다. 클럽은 23 명 미만의 플레이어와 2 명 이상의 플레이어 만 같은 위치에서 플레이 할 수 있습니다.datamapper를 사용하는 객체 연결
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
스크립트의 끝에서 끝
, 내가 기대하는 모든 클럽은 (22 명) 선수가있다. 왜 그렇게 안좋은거야?
편집 : 스크립트의 끝에서 나는 마지막 클럽에 할당 된 22 명 선수 (20)와 10,000 플레이어를 얻을
각 클럽에는 대본이 끝날 때 몇 명의 선수가 있습니까? – Larsenal
나는 그 질문을 편집했다. 대본이 끝나면 마지막 클럽 (20 명)에 22 명의 플레이어 만 배정되고 10000 명의 플레이어가 생성됩니다 – marcosdsanchez
나는 좋은 대답을 얻었습니다. – Larsenal