I would like to sort through a table (Lessons) and group them by Subject(A field in the Lesson’s table). Then display them by subject.
So I would have a header: Subject
Then it would display the image, title and description for each item in that subject.
Then it would move onto the next Subject and so on up to 5 items
For example:
Math
[img] [img] [img] ...
title title title ...
desc desc desc ...
Science
[img] [img] [img] ...
title title title ...
desc desc desc ...
etc.....
In the controller how would you go about sorting and then dividing up?
How do you display this in the view if you have a dynamic amount of subjects?
The meat of your needs are met by
Enumerable#group_by:Whatever you return from group_by will be the key for your hash, while the value (
subj_lessons) is an array of all the items that matched that grouping.Modify this to fit your actual data and your output needs.