开发者

How do I do a grouping by year?

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 ever开发者_Python百科y 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.


You can use group_by (see API) like (of the top of my head

<% @all_books.group_by(&:year).each do |year, book| %>

   ...
<% end %>


def year
  self.created_at.strftime('%Y')
end

< % @all_books.group_by(&:year).each do |year, book| %>

Year < %= year %>
# render books here

< % end %>

What say?


You can use group_by for convenience, but your need can be better served by relying on DB for sorting and a each loop. This avoids the cost of client side sorting and hash manipulations for grouping.

Somewhere in your controller

@all_books = Book.all(:order => "publish_date DESC")

In your view

<%year = nil
  @all_books.each do |book| 
    if year.nil? or year > book.publish_date.year 
      year = book.publish_date.year 
%>
      <h1> <%=year%><h1>
  <%end % >
  <%=link_to book.title + ", (PDF, " + get_file_size(book.size) + ")" %>
<%end %>


The quick and dirty approach is to simply group_by the year and iterate over those:

@all_books.group_by { |b| b.created_at.year }.each do |year, books|
  # All books for one year, so put heading here
  books.each do |book|
    # ...
  end
end

This requires sorting within the Rails application, so you will need to retrieve all relevant records in order to have the data properly organized. To do the sort on the server you will probably need to introduce a year column and keep it in sync with the created_at time.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜