Mysql 使用ID从表中检索值

Mysql 使用ID从表中检索值,mysql,ruby-on-rails,ruby,model,Mysql,Ruby On Rails,Ruby,Model,嗨,我有一张团队表和一个夹具模型: 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 您应该

嗨,我有一张团队表和一个夹具模型:

 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

您应该将算法的逻辑放入夹具模型中;用静态方法将其模块化,如

def self.show_fixtures
   #add algo here
end
但是您必须修改代码以返回数组或其他数据结构。 现在你可以做了

Fixture.show_fixtures
无论何处-在任何视图中,在控制器中等以获得匹配

还可以定义用于显示装置的专用视图。首先,在FixtureController中添加一个操作

def show_fixtures
  @list = Fixture.show_fixtures
end       
然后,添加view/views/fixture/show_fixtures.html.your_扩展名
在视图中,您可以迭代@list数组,并为fixture中的每个匹配呈现一个分部。

假设您的数据库中有20个团队条目(具有属性
名称
)(或更多):


我不明白你的代码在做什么,但正如你在评论中所说的,我建议你这样做

将此方法添加到夹具中。调用夹具。创建夹具

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 
def self.create_夹具
周\配对=[]
teams=Team.all.map(&:name)
fixed_team=teams.shift#算法中描述的固定竞争对手
团队.长度.时间做| i|
#创建算法中列出的两个组
团队=团队。轮换
周团队=团队.dup.unshift(固定团队)
第一组,第二组=周组。每个切片(周组。长度/2)。到
第二组,反向!
周与周配对

您到底需要什么?如何在视图中输出?我仍然不知道上面的代码应该实现什么…我有一个团队模型,其中包含一个团队列表,带有id和团队名称,我的输出是生成装置,因此在我的应用程序中,我希望能够看到团队[I]\u名称与团队[I]_上面代码输出中每对的名称。我的目标是为循环赛联盟创建赛程。每支球队对另一支球队比赛一次,一支球队每周只能比赛一次。有一种方法,我可以将球队分配给主队和客队,获得两支球队中的第一个价值作为主队,第二个价值作为客队,而无需将它们全部打印出来。你想要主队还是客队?是的,目前,我有一个基本视图,可以手动创建固定装置。我的固定装置视图显示了5件事情:主场、客场、主场、客场和结果。所以每一对的第一个id必须是主队,第二个客队,如果你明白的话?
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
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 %>