All threads / Order by score using Pagy pagination?

Ask A Question

Notifications

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(
        Driver.select(
            'drivers.*',
            '(drivers.no_races + drivers.no_poles + drivers.no_podiums + drivers.no_wins) AS score'
        ).reorder('drivers.score DESC'),
        page: params[:page],
        items: 16
    )
  end

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 - http://29qg.hatchboxapp.com/drivers

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 53,130+ developers who get early access to new tutorials, screencasts, articles, and more.

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

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2021 GoRails, LLC. All rights reserved.