Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignores :order option. #573

Open
pienkowskip opened this issue May 15, 2018 · 3 comments
Open

Ignores :order option. #573

pienkowskip opened this issue May 15, 2018 · 3 comments

Comments

@pienkowskip
Copy link

pienkowskip commented May 15, 2018

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

@masak2009
Copy link

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 ;-)

@ughstudios
Copy link

Is there a fix??

@djcuze
Copy link

djcuze commented Dec 11, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants