Forum: Ruby on Rails using pagination to group pages by attribute value

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
006a4831843b48f6102d5d3cf92ae283?d=identicon&s=25 Shilo Ayalon (gte351s)
on 2009-03-27 15:41
Hi -

I'm trying to group records based on some attribute they or an
association of their's have. The simplest example I could think of to
drive my point is this:

Assume the following model associations (pardon my butchering of rails)
-

Faculty has_many :courses
Course belongs_to :faculty

I want to paginate courses based on the name of the faculty they belong
to.

The paging may look something like this (instead of page numbers):

# in the actual view

 << prev   Archeology Botanics ... |Math| ... Zoology   next >>

   # listing all courses of current page


I'd appreciate help on how to accomplish that. I know will_paginate does
numbering, but I couldn't figure out if it can do the above.
E00fa5dff361e0bed4b488d874960889?d=identicon&s=25 Danimal (Guest)
on 2009-03-31 18:16
(Received via mailing list)
Shilo, you might be interested in "group_by":
http://weblog.rubyonrails.org/2006/3/1/new-in-rail...

The first example groups by an attribute (in the above case, "day").
Try it in script/console first to see how the data structure is
returned. It's a nested array of key, values where the key is the
attribute and the value is an array of records that are in that group.
You can iterate through it with the each call as in that example.

HTH!

-Danimal

P.S. I've used group by with will_paginate and it works just fine

On Mar 27, 8:41 am, Shilo Ayalon <rails-mailing-l...@andreas-s.net>
This topic is locked and can not be replied to.