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

Reply

do you have some default_scope in Driver?

Reply

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

Reply

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

Reply
Join the discussion
Create an account Log in

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

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

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