Rails/Kaminari - 如何订购分页数组? [英] Rails / Kaminari - How to order a paginated array?

查看:58
本文介绍了Rails/Kaminari - 如何订购分页数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在排序一个数组时遇到问题,我已经用 Kaminari 成功地对它进行了分页.

I'm having a problem ordering an array that I've successfully paginated with Kaminari.

在我的控制器中,我有:

In my controller I have:

@things = @friend_things + @user_things

@results = Kaminari.paginate_array(@things).page(params[:page]).per(20)

我想要由 :created_at 排序的最终 @results 数组,但没有运气获得排序以使用 Kaminari 提供的通用数组包装器.有没有办法在 Kaminari 包装器中设置顺序?否则最好的方法是什么?谢谢.

I want to have the final @results array ordered by :created_at, but have had no luck getting ordering to work with the generic array wrapper that Kaminari provides. Is there a way to set the order in the Kaminari wrapper? Otherwise what would be the best way? Thanks.

推荐答案

您可以在将元素发送到 Kaminary 之前对其进行排序,如下所示:

You could sort the elements before sending it to Kaminary, like this:

@things = @friend_things + @user_things
@things.sort! { |a,b| a.created_at <=> b.created_at }
@results = Kaminari.paginate_array(@things).page(params[:page]).per(20)

@things = @friend_things + @user_things
@things.sort_by! { |thing| thing.created_at }
@results = Kaminari.paginate_array(@things).page(params[:page]).per(20)

这篇关于Rails/Kaminari - 如何订购分页数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆