ruby 关于 rails 和 Kaminari 分页

ruby on rails and Kaminari pagination

我正在使用 Kaminari 对许多学校对象进行分页,但我必须按评级对它们进行排序,但出了点问题:分页没问题,但 gem 对分页的每一页按评级排序,而不是所有页面中出现的所有学校,例如:

page 1 -> school 1 - rate 20, school 2 - rate 10, school 3 - rate 0
page 2 -> school 4 - rate 12, school 5 - rate 6, school 7 - rate 0
page 3 -> ...

我想要:

page 1 -> school 1 - rate 200, school 2 - rate 120, school 3 - rate 100
page 2 -> school 4 - rate 90, school 5 - rate 90, school 6 - rate 85
...

这是我的代码:

def index
  @schools = School.where(:subscription.exists => false).page(params[:page]).per(50)
  @schoolss = @schools.sort_by(&:ratings_count).reverse
end

和视图

  <tbody>
    <% @sch.each do |school| %>
        <tr>
          <th> <%= link_to school.title, school.path_school_profile %> </th>
          <th> <%= school.city %> </th>
          <th> <%= school.ratings.count %> avis | <%= school.avg_ratings_score_round %>/5 </th>
        </tr>
    <% end %>
  </tbody>
</table>
<%= paginate @schools %>
<%= page_entries_info @schools %>

有人可以帮助我吗?

您将@schools 排序为新变量(@schoolss),为什么不使用它??

将您的索引控制器更改为此

def index
  @schools = School.where(:subscription.exists => false).order_by(:ratings_count => 'desc').page(params[:page]).per(50)
end