0

Im creating a time line with some columns from a table. At the moment is the time line just display each column ordered by the "created_at" field. But i want slice it for each day like this:

       TODAY
content -|
         |
         |- content
         |
content -|
         |
      YESTERDAY
         |
         |- content
         |
content -|

At the moment am i using this code

<ul>
<% @timeline.each do |timeline| %>
    <li>
        <%= timeline.title %>
    </li>
<% end %>
</ul>

I want to do something like the "each_slice(5)" function. Hope anybody can tell me how to do this. Thanks in advance.

Simone Carletti
  • 173,507
  • 49
  • 363
  • 364
niiicolai
  • 73
  • 2
  • 12
  • http://stackoverflow.com/questions/4987392/how-do-i-group-by-day-instead-of-date – max Nov 10 '14 at 22:04

1 Answers1

2

You can group your array with ruby group_by:

@timeline.group_by { |el| el.created_at.to_date.to_s(:db) }

Note: I assume that @timeline already sorted.

Probably, this method will be reusable, so better place it in models like:

def self.to_grouped_by_day
  group_by { |el| el.created_at.to_date.to_s(:db) }
end

And than call it in the controller.

dubadub
  • 3,312
  • 3
  • 23
  • 28