2

嗨,我有一个团队表,然后是一个夹具模型:

 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
4

3 回答 3

1

假设name您的数据库(或更多)中有 20 个 Teams 条目(具有属性):

teams = Team.first(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)

   puts "Week #{i+1}: "
   weeks_pairings.each do |pair_of_teams|
     puts "#{pair_of_teams[0].name} vs. #{pair_of_teams[1].name}"
   end
end
于 2013-01-29T17:54:38.157 回答
0

我没有得到你的代码在做什么,但正如你在评论中所说,我建议你这样做。

在Fixture中添加此方法。调用Fixture.create_fixture

def self.create_fixture
  weeks_pairings = []
  teams = Team.all.map(&:name)
  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)
  end
    weeks_pairings #You can use this in view
end 

鉴于:

 <% Fixture.create_fixture.each_with_index do |week, games_list| %>
   <%= "<h3> Week #{week+1} </h3>" %>
   <ul>
     <% games_list.each do |teams| %>
       <li><b><%= "#{teams.first} Vs #{teams.last}" %></li>
     <% end %>
   </ul>
  <% end %>  
于 2013-01-29T17:57:12.410 回答
0

您应该将算法的逻辑放入您的 Fixture 模型中;将其模块化为静态方法,例如

def self.show_fixtures
   #add algo here
end

但是您必须修改代码以返回数组或其他一些数据结构。现在你可以做

Fixture.show_fixtures

无论在哪里 - 在任何视图中,在控制器等中获得对决。

您还可以定义一个专用视图来显示灯具。首先,在 FixtureController 中添加一个动作

def show_fixtures
  @list = Fixture.show_fixtures
end       

然后,添加视图 /views/fixture/show_fixtures.html.your_extension 在视图中,您可以迭代 @list 数组并为夹具中的每个匹配渲染一个部分。

于 2013-01-29T17:54:30.620 回答