简体   繁体   English

使用 Pagy 分页按分数排序?

[英]Order by score using Pagy pagination?

I am currently trying to order some ActiveRecord records by score.我目前正在尝试按分数订购一些 ActiveRecord 记录。 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.我正在使用 Pagy 进行分页。 As the code shows, I do a query to select all drivers and then add together 3 columns in the table as 'score'.如代码所示,我执行查询以选择所有驱动程序,然后将表中的 3 列加在一起作为“分数”。 I then want to order by score going from high to low and show 16 records per page.然后我想按分数从高到低排序,每页显示 16 条记录。 When the page loads it seems to order by driver id.当页面加载时,它似乎按驱动程序 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样机 - http://29qg.hatchboxapp.com/drivers

Ok so it was a simple error in the end.好的,所以最后是一个简单的错误。 "score" is ambiguous because i was using score as an alias and i also had a column in the table called score. "score" is ambiguous因为我使用分数作为别名,而且我在表格中还有一列叫做分数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM