Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you use Kaminari to paginate a page by a number value?

I've set up Kaminari so that it can accurately paginate by post title if i do this:

def index
...
@posts = Post.order('title').page(params[:page]).per(5)
...
end

i also have <% paginate %> in the view

but if I try something like this

@posts = Post.order('pageviews').page(params[:page]).per(5)

it stops working. There are no bugs or errors, but it just appears to be sorted arbitrarily. Possibly by date. How come?

like image 747
Jon Avatar asked Jan 21 '26 17:01

Jon


2 Answers

Firstly, is it correctly sorted without Kaminari? I mean, how is Post.order('pageviews') sorted?

Next, can you check the output SQL? How is it sorted if you run

Post.order('pageviews').page(1).to_sql

result on your DB console?

like image 137
Akira Matsuda Avatar answered Jan 23 '26 07:01

Akira Matsuda


May be you need desc at order clause? because currently it will order 0 views first.

 Post.order('pageviews desc')
like image 26
YOU Avatar answered Jan 23 '26 05:01

YOU



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!