I’m trying to group contents that belong on a same date together for display, such that the date only gets posted once and in chronological order.
Something like:
14th October 2009
Item 3
Item 3 content
Item 2
Item 2 content
13th October 2009
Item 1
Item 3 content
How can I display that in view? (Assume that @items is being passed from the controller, which contains all the items)
I’ve tried group_by, but I can’t get it to work as it seems to be arranging itself by the value of the day itself rather than together with the month.
The code in question: http://github.com/davidchua/Political-Watch/blob/master/app/views/items/index.html.erb
To see the problem in a live deployment, its at http://political-watch.org
1) Replace the line 5 of app/controllers/items_controller.rb with:
2) Replace line 3-14 of app/views/items/index.html.erb with:
In this approach you are using the DB for sorting and a simple comparison for grouping.
PS: I don’t think it is a good idea to name your database columns as ‘date’. In some of the databases ‘date’ is a reserved keyword.