mislav/will_paginate

Ignores `:order` option.

pienkowskip opened this issue · 3 comments

The paginate method seems to ignore :order option which is provided via set_default_order method in Hobo::Model.

This is bug for 3 years! It not works in Rails 4 too.

Example:

invoices = Invoice.active.order("created_at DESC").paginate(:page => params[:page], :per_page => 2)

It always sort invoices like created_at ASC.

In will_paginate for Rails 2 same part of code works like a charm ;-)

Is there a fix??

I ran into a similar problem with our Rails 5.1 app.

We were facing an issue where the last record of page 1, would show as the first record on page 2 if we were attempting to .order our results by their first name.

I opted to do a side-by-side comparison. One using the .paginate method, and the other converting our ActiveRecord collection to an array, sorting it the same way we were with .order using Array#slice.

NB: The numbers in the tables here refer to the index of the object in the base collection

Example for Page 1 Comparison

Paginated method

def paginated_collection
    collection.order('first_name').paginate(page: 1, per_page: 15)
end

Array#slice method

def sliced_array_collection
    collection.to_ary.sort_by(&:first_name).slice(0..14)
end

image

Example for Page 2 Comparison

Paginated method

def paginated_collection
    collection.order('first_name').paginate(page: 2, per_page: 15)
end

Array#slice method

def sliced_array_collection
    collection.to_ary.sort_by(&:first_name).slice(15..29)
end

image