Reputation: 14509
I am trying to sort an index of submissions.
Each submission has_one :score
and score belongs_to :submission
submission belongs_to :contest
and contest has_many :submissions
the row I want to sort by is submission.score.subtotal
in submissions>index
I have tried this:
@submissions = submission.find(:all, :include => :score).sort_by { |s| s.scores.sub_total }.paginate(:per_page => 10, :page => params[:page])
I have assigned scores to all the submissions in contest one, but when I try to bring up submissions>index for contest one (assigned through params), I'm getting an error:
NoMethodError at /submissions
undefined method `sub_total' for nil:NilClass
I'm pretty sure I need to check for nil somewhere, but I'm missing where that would be.
More information:
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
Upvotes: 0
Views: 116
Reputation: 2243
This code should do what you need:
@submissions = Submission.find(:all, :joins => :score, :order => 'scores.sub_total DESC').paginate(:per_page => 10, :page => params[:page])
I hope this helped you
Upvotes: 1