Ask A Question


You’re not receiving notifications from this thread.

Order by score using Pagy pagination?

Tom Dringer asked in Rails

Hi everyone. I am currently trying to order some ActiveRecord records by score. In my controller i have:

def index
    @pagy, @drivers = pagy(
            '(drivers.no_races + drivers.no_poles + drivers.no_podiums + drivers.no_wins) AS score'
        ).reorder('drivers.score DESC'),
        page: params[:page],
        items: 16

I am using Pagy for the pagination. As the code shows, I do a query to select all drivers and then add together 3 columns in the table as 'score'. I then want to order by score going from high to low and show 16 records per page.
When the page loads it seems to order by driver id. I can't see anywhere else that i have an order by, but i did add reorder to override anything else.
Anyway for whatever reason i'm stuck with the wrong ordering. Any direction is appreciated :-)

Mockup -


do you have some default_scope in Driver?


I've just checked and i don't have default scope anywhere


@joshBrody - just check default scope is only going to be in the model or the controller i guess?

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 71,933+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more. Icons by Icons8

    © 2023 GoRails, LLC. All rights reserved.