Rails / Kaminari - How to order a paginated array?

守給你的承諾、 提交于 2019-12-11 09:16:16

问题


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)

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.


回答1:


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)

or

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


来源:https://stackoverflow.com/questions/16326855/rails-kaminari-how-to-order-a-paginated-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!