我试图通过相当复杂的关联来订购查询并且没有运气。通过关联订购
下面是该机型的相关部分...
class Athlete < ActiveRecord::Base
has_many :participations
has_many :matches, :through => :participations
scope :active, -> { where(active: true) }
end
class Participation < ActiveRecord::Base
belongs_to :athlete
belongs_to :match
end
class Match < ActiveRecord::Base
has_many :participations, :dependent => :destroy
has_many :athletes, :through => :participations
scope :unplayed, -> { where("started_at is NULL and year=2015") }
end
我需要通过“athlete.matches.unplayed.count”订购运动员的集合。所以我有has_many:通过关联来处理以及Match上的“未播放”范围。
在回应评论时增加清晰度。具体地讲,我有这样的表中的图:
<% @athletes.each do |athlete| %>
<% match = athlete.matches.in_progress.first %>
<tr>
<td><%= athlete.full_name %></td>
<td><%= athlete.idle? ? "Idle" : "Playing" %></td>
<% if athlete.idle? %>
<td></td>
<td></td>
<td></td>
<% else %>
<td><%= match.sport_name %></td>
<td><%= match.arena_name %></td>
<td><%= match.elapsed_time_string %></td>
<% end %>
<td><%= athlete.matches.unplayed.count %></td>
</tr>
<% end %>
我想表,因此@athletes,由最后一列我显示“athletes.matches.unplayed.count”排序。
从控制器,@athletes是:提前
@athletes = Athlete.active
谢谢!
'由什么order'?期望的结果是什么? – 2015-02-08 12:19:15
我需要通过“athlete.matches.unplayed.count”在栏杆中返回的内容来命令返回的一组运动员。 – 2015-02-08 12:22:52
还不清楚...... – 2015-02-08 12:25:28