0

In my controller, I have:

@bills = Bill.where(:param1 => x, :param2 => y).sort_by {|u| u.created_at}.reverse.paginate(:page => params[:page])

And I get the above-mentioned error when I try to render. Getting rid of the where, sort and reverse clauses gets rid of the problem but I need them. How can I have both?

user1436111
  • 2,111
  • 7
  • 24
  • 41

3 Answers3

0

When you call sort_by {|u| u.created_at}, you fetch data from db to array and sort it in ruby side.

If you want paginate the ruby array data try this:

@bills = Kaminari.paginate_array(Bill.where(:param1 => x, :param2 => y)).page(params[:page]).per(PER_PAGE_RECORDS)`

but the better way sort yours data in the DB query

@bills = Bill.where(:param1 => x, :param2 => y).order("created_at DESC").paginate(:page => params[:page])
kr00lix
  • 3,284
  • 1
  • 17
  • 11
0

use AR chains. sort_by results in an array.

@bills = Bill.where(:param1 => x, :param2 => y).order('created_at DESC').paginate(:page => params[:page])
jvnill
  • 29,479
  • 4
  • 83
  • 86
0

I use the gem meta_search (usage) in conjunction with the gem will_paginate (link). I think it could help you here.

Community
  • 1
  • 1
tingel2k
  • 392
  • 1
  • 12