Is it possible to use will_paginate with GROUP BY?

℡╲_俬逩灬. 提交于 2019-12-10 14:18:43

问题


I have a huge database and I want to reduce the query response time by using will_paginate.

I am trying to group my entries by a column and then use the will_paginate to put results into different pages.

I try to do this

@list= Persons.find_by_sql(select).group_by {|t| t.gender}
@detail= @list.paginate(:page => params[:page], :per_page => 30)

but it gives me this error:(undefined method paginate for<Hash:0x3de9930>):

Anyone know how to solve this problem?

[Additional information]

Someone on the will_paginate forum says this:

Because you already have to load all the users from the database in order to perform the group_by, my suggestion is simply to forget about pagination and output them as-is.

Does it mean there is no point to use GROUP_BY while using will_paginate?


回答1:


@paginated_lists = Persons.find_by_sql(select).paginate(:page => params[:page], :per_page => 30)
@details = @paginated_lists.to_a.group_by { |t| t.gender }

Try this it should work it worked for me :) use @paginated_lists for paginating.



来源:https://stackoverflow.com/questions/12222738/is-it-possible-to-use-will-paginate-with-group-by

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