0

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
Brian McDonough
  • 13,829
  • 4
  • 19
  • 23

1 Answers1

1

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

Code-Source
  • 2,215
  • 19
  • 13
  • Getting an error on that one: `ActiveRecord::StatementInvalid at /submissions PG::Error: ERROR: missing FROM-clause entry for table "score" LINE 1: ...id" WHERE "submissions"."contest_id" = 1 ORDER BY score.subt... ^ : SELECT "submissions".* FROM "submissions" INNER JOIN "scores" ON "scores"."submission_id" = "submissions"."id" WHERE "submissions"."contest_id" = 1 ORDER BY score.subtotal DESC` I added the controller information in the question because there's a contest.id involved that may throw the query off – Brian McDonough Feb 08 '13 at 00:48
  • Sorry my bad, i edited my answer. It should be ":order => 'scores.sub_total'" – Code-Source Feb 08 '13 at 01:04
  • Okay, that works, except it will not paginate: `NoMethodError at /submissions undefined method `paginate' for #` – Brian McDonough Feb 08 '13 at 02:38
  • Well yes paginate don't accept complex select. You should have a look at this solution: http://stackoverflow.com/questions/2868484/rails-using-will-paginate-with-a-complex-association-find – Code-Source Feb 08 '13 at 03:07
  • I had to add `require 'will_paginate/array'` to the gemfile to make it work. Thank you for your help. Much appreciated! – Brian McDonough Feb 08 '13 at 03:28
  • Scratch that, I had to create an initializer and then drop in `require 'will_paginate/array'` It did not belong in the gemfile, obvious mistake. See this thread http://stackoverflow.com/questions/4352895/ruby-on-rails-will-paginate-an-array – Brian McDonough Feb 08 '13 at 03:33