我正在尝试对提交的索引进行排序。
每次提交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