Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby On Rails Mongoid Group By

For group by date I used group_by method.

Example: Product.all.group_by {|d| d.created_at } # return Hash 

But kaminari not support Hash. I use Mongoid and me need group by date with page navigation (kaminari). How do it?

like image 901
nikdev Avatar asked Mar 26 '13 15:03

nikdev


1 Answers

Kaminari only support paginate on array for example

Kaminari.paginate_array(an_array).page(1).per(10)

group_by is not mongoid method, it Array's method, it is grouping all data in memory. In order to use mongoid grouping feature, you need use mongodb map/reduce.

If you do need group result in memory, and show it in view, you need manually convert hash to Array

like image 142
allenwei Avatar answered Oct 04 '22 22:10

allenwei