2

こんにちは、teams テーブルとフィクスチャ モデルがあります。

 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 チームに基づいてフィクスチャをビューに表示しようとしていますが、方法がわかりませんか? したがって、私の出力は、ID 1 のホーム チーム team_name と、備品 1 の ID 2 のアウェイ チーム team_name などになります...

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

DBに 20 個の Teams エントリ (属性を持つname) があるとします (またはそれ以上):

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

フィクスチャ モデルにアルゴリズムのロジックを配置する必要があります。次のような静的メソッドでモジュール化します

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 に答える
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 に答える