Wilson
Wilson

Reputation: 181

How to list/group post by date in rails?

I have a table with a list of events (postings). All the event have a date datatype with start_date and end_date of the events.

I to display the list of events with the events that will be happening first. So for example, I have an event that's starting on November 12th and then a few on November 13, 14 and so on. I want to display the ones that will be happening first. How can I reorder it?

Here's my code now:

<tbody>
    <% @events.each do |event| %>
      <tr>
        <td><%= event.image %>" height="50" width="50"></td>
        <td> <%= link_to event.title, event %></td>

        <td><%= event.category %></td>


        <td> <%= event.start_date %> / <%= event.end_dates %></td>




      </tr>

My controller is just:

 def index
    @events = Event.all
  end

Upvotes: 0

Views: 90

Answers (1)

Achilles
Achilles

Reputation: 766

In your controller:

def index
  @events = Event.order(:start_date)
end

You may want to go through the Active Record Basics Page

Upvotes: 2

Related Questions