Search code examples
ruby-on-railsrubypaginationkaminari

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?


Solution

  • 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?