嗨,我有一个团队表,然后是一个夹具模型:
class Fixture < ActiveRecord::Base
attr_accessible :away_score, :away_team_id, :home_score, :home_team_id, :result, :week
belongs_to :home_team, :class_name => 'Team'
belongs_to :away_team, :class_name => 'Team'
end
然后我有一个程序可以根据他们的团队 ID 生成固定装置,但我不确定在我的 rails 应用程序中的何处添加它,我试图根据我已经创建的 20 个团队在视图中显示固定装置,但不确定如何?所以我的输出将是主队 team_name id 1 和客队 team_name id 2 用于夹具 1 等......
teams = Array(1..20)
fixed_team = teams.shift #The fixed competitor described in the algorithm
teams.length.times do |i|
#Create the two groups listed in the algorithm
teams = teams.rotate
week_teams = teams.dup.unshift(fixed_team)
first_group, second_group = week_teams.each_slice(week_teams.length/2).to_a
second_group.reverse!
weeks_pairings = first_group.zip(second_group)
#Output the week's pairings
puts "Week #{i + 1}: #{weeks_pairings}"
end
#Output:
#=> Week 1: [[1, 2], [3, 20], [4, 19], [5, 18], [6, 17], [7, 16], [8, 15], [9, 14], [10, 13], [11, 12]]
#=> Week 2: [[1, 3], [4, 2], [5, 20], [6, 19], [7, 18], [8, 17], [9, 16], [10, 15], [11, 14], [12, 13]]
#=> etc