0

我正在尝试对提交的索引进行排序。

每次提交has_one :score和分数belongs_to :submission提交belongs_to :contest和比赛has_many :submissions

我要排序的行在submission.score.subtotal提交>索引中

我试过这个:

 @submissions = submission.find(:all, :include => :score).sort_by { |s| s.scores.sub_total }.paginate(:per_page => 10, :page => params[:page])

我已经为第一场比赛的所有提交分配了分数,但是当我尝试为第一场比赛调出提交>索引(通过参数分配)时,我收到一个错误:

NoMethodError at /submissions
undefined method `sub_total' for nil:NilClass

我很确定我需要在某个地方检查 nil ,但我错过了那个地方。

更多信息:

def index
  contest_id = params[:contest_id]
  @contest = Contest.find(contest_id)
  submission = Submission.where(:contest_id => params[:contest_id])
  if params[:search].blank?
   @submissions = submission.find(:all, :include => :score).sort_by { |s| s.score.sub_total }.paginate(:per_page => 10, :page => params[:page])
  else 
   @submissions = submission.search(params[:search]).paginate(:per_page => 10, :page => params[:page])
  end
   @search = params[:search] 
end
4

1 回答 1

1

这段代码应该做你需要的:

@submissions = Submission.find(:all, :joins => :score, :order => 'scores.sub_total DESC').paginate(:per_page => 10, :page => params[:page])

我希望这对你有帮助

于 2013-02-08T00:41:46.523 回答