How do I do a grouping by year?

Posted by kibyegn on Stack Overflow See other posts from Stack Overflow or by kibyegn
Published on 2010-03-23T07:43:14Z Indexed on 2010/03/23 7:53 UTC
Read the original article Hit count: 308

Filed under:
|
|
|

I have a books model with a date type column named publish_date. On my views I'm iterating through the books and I want to group the books by year such that I have a heading for every year and books that were published on that year to be listed below the year heading.

So by starting with "2010" all books published on 2010 would be listed, then another heading "2009" with all books published in 2009 listed below it and so forth.

<% @all_books.each do |book| %>
   <%=link_to book.title + ", (PDF, " + get_file_size(book.size) + ")" %>
<% end %>

By doing a book.publish_date.strftime("%Y") I am able to get the year but I do not know how to group the entries by year. Any help on this would be appreciated.

© Stack Overflow or respective owner

Related posts about rails

Related posts about ruby