我想显示一个表单来添加或编辑结果(如果存在)。我有一个n * n表,其中单元格中有n个团队由结果给出。这有多好?
view/championship/show.erb
<table class="table table-bordered">
<tr><th> </th>
<% @teams.each do |teamhome| %>
<th class="vertical"><%= teamhome.name %></th>
<% end %>
</tr>
<% @teams.each do |teamguest| %>
<tr>
<th><%= teamguest.name %></th>
<% @teams.each do |teamhome| %>
<%if teamhome == teamguest %>
<td bgcolor = '#F9F9F9'>
<% else %>
<td>
#render form
</td>
<% end %>
<% end %>
</tr>
<% end %>
</table>
模型
class Score < ActiveRecord::Base
attr_accessible :team1_score, :team2_score, :team1_id, :team2_id, :team1, :team2
belongs_to :team1, class_name: 'Team', foreign_key: :team1_id
belongs_to :team2, class_name: 'Team', foreign_key: :team2_id
def self.to_arr
score_arr = self.find(:all, :select => 'team1_id, team2_id, team1_score, team2_score').to_a
end
end
class Team < ActiveRecord::Base
attr_accessible :name
belongs_to :championship
has_many :team1_scores, class_name: 'Score', foreign_key: :team1_id
has_many :team2_scores, class_name: 'Score', foreign_key: :team2_id
validates :name, presence: true, length: { maximum: 100 }
validates :championship_id, presence: true
end
模式
“team1_id” | “team2_id” | “TEAM1_SCORE” | “TEAM2_SCORE”
答案 0 :(得分:0)
一种方法是将所有分数加载为{ [:team1_id, :team2_id] => [:team1_score, :team2_score] }
形式的哈希:
@scores = Score.all.inject({}) do |m, s|
m[[s.team1_id, s.team2_id]] = [s.team1_score, s.team2_score]
m
end
现在,您可以为每个家庭/客人配对呈现分数:
<% scores = @scores[[teamhome.id, teamguest.id]] %>
<%= "#{scores.first} / #{scores.last}" if scores %>
这种方法的优点是你可以通过散列键找到匹配的分数,即使对于大量数据也应该相当快。缺点是这个哈希加载所有分数 - 即使那些未包括在@championship
中的分数。如果要避免此问题,可以构建范围以缩小相关团队的分数。然后,只需拨打Score.your_scope
而不是Score.all
。
另一种方法(不需要构建哈希)是直接从主队获得分数并检测该客队团队的适当匹配:
<% scores = teamhome.team1_scores.detect{|s| s.team2_id == teamgues.id }
<%= "#{scores.team1_score} / #{scores.team2_score}" if scores %>
如果您没有过多的数据,这种简单的方法就足够了。只需确保在最初加载团队时急切加载主队得分,否则您的表现将受到N + 1个查询的影响。
<强>更新强>
在这两种方法中,分数都呈现为"home_team_score / guest_team_score"
,但仅在找到匹配的分数时才会显示。如果未找到,则不呈现任何内容 - 这是由if scores
条件语句引起的。如果要渲染其他内容,可以更改条件以使用三元运算符。以下示例采用第二种收集分数的方法:
<%= scores ? "#{scores.team1_score} / #{scores.team2_score}" : "No Score" %>
如果找不到匹配的分数,这将呈现'No Score'
。