+++ date = "2007-01-12" title = "Rails: Group results by week (using group_by)" tags = ["General", "Everything", "RubyOnRails", "Features"] slug = "rails-group-results-by-week-using-group_by" +++ The Enumerable class in Rails contains a method named 'group_by'. This method is pure magic for a developer's point of view. I'll give you a simple example that shows the power of group_by. Let's say you have a table 'posts' containing blog posts. Now, you normally show these chronologically a few at a time. Nothing special there. For some special overview page, you want to group your posts by week. With normal ActiveRecord operations this would be quite an elaborate task. But with group_by from Enumerable, it becomes child's play. First of all, in the controller, just get all the posts you need. In this case, all of them: Controller: def list @posts = Post.find :all end As you can see, I perform no ordering or whatsoever here. Now, in your view you normally would iterate over all posts like this: <%= render :partial => 'post', :collection => @posts %> But, as I said, we want to group the posts by week. To make life easy, I add a method to the Post class that returns the week number in which a post was written: Model Post: def week self.created_at.strftime('%W') end Now, the magic will happen in our view: <% @posts.group_by(&:week).each do |week, posts| %>