0

I'm trying to do pagination using the will_paginate gem:

@books = Book.joins(:ads).last(20).page(params[:page]).per_page(10)

But I'm getting this error: undefined method `page' for #<\Array:0x007fc3ef37d308> and I can't seem to figure out what's wrong. Pagination works like a charm in other actions.

Thanks! :)

oyvindhauge
  • 3,496
  • 2
  • 29
  • 45
  • Similar to http://stackoverflow.com/questions/13187076/paginating-an-array-in-ruby-with-will-paginate – rlecaro2 Jan 29 '14 at 19:28

2 Answers2

0

Don't use last as that will trigger the query execution. Use reverse_order and limit instead.

Book.joins(:ads).reverse_order.limit(20).page(params[:page]).per_page(10)
PinnyM
  • 35,165
  • 3
  • 73
  • 81
0

If you still want to paginate array:

require 'will_paginate/array'

and then use

Array#paginate

https://github.com/mislav/will_paginate/wiki/Backwards-incompatibility (the very bottom of the page)

sources:

https://github.com/mislav/will_paginate/blob/2-3-stable/lib/will_paginate/array.rb https://github.com/mislav/will_paginate/blob/master/lib/will_paginate/array.rb

trushkevich
  • 2,657
  • 1
  • 28
  • 37