Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use will_paginate with GROUP BY?

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?

like image 841
John Powel Avatar asked Nov 04 '22 17:11

John Powel


1 Answers

@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.

like image 78
Shilpi Agrawal Avatar answered Nov 08 '22 05:11

Shilpi Agrawal