Search code examples
ruby-on-railsrubyruby-on-rails-3.1enumerable

How to display a table segregated by dates in a model


Using Enumerable, I am trying to display dates in an organized event calendar by dates. Not as a standard 7 grid calendar.

Trying to get:

December 1, 2011 Event 1 Event 2 Event 3

December 2, 2011 Event 4 Event 5

December 3, 2011 Event 6

...

Currently Enumerable confuses the hell outta me. Thanks for any guidance--

EDIT Dec.22,2011

Current Data:

Controller string-

@events = Event.where('start >= ?', Date.today).paginate(:per_page => 15, :page => params[:page])

View call-

<% @events.each do |event| %>
  <table stuff here>
<% end %>

Method- Have not specified anything enumerable on method as of now---

My table includes the following fields; :title, :venue, :address (is geocoded), :about (text), :start (datetime function)


Solution

  • Update for question update: So you have a bunch of event objects in @events so you could use a group_by:

    @events_by_date = @events.group_by { |e| e.start.to_date }
    

    That would give you a Hash in @events_by_date with dates as the keys and arrays of Event objects as the values.

    Then you'd want an array of arrays of Dates to match your month and drive the table from that array:

    <table>
        <% @month.each do |week| %>
            <tr>
                <% week.each do |day| %>
                    <td>
                        <% if day %>
                            <%= day.day %>
                            <% @events_by_date[day].to_a.each do |e| %>
                                <%= e.about %>
                            <% end %>
                        <% end %>
                    </td>
                <% end %>
            </tr>
        <% end %>
    </table>
    

    There would be some formatting involved of course but the structure would be similar.