Topic: If Else Statement in Views

I want to simulate the way craigslist displays the date of submissions.
PunBB bbcode test

I want to display submission date for the entries, but I don't want to see duplicates of the submission date.

My pseudocode goes something like this, but not sure how to implement it.

In view file,

<% @submissions.each do |submission| %>                                   #while loop going through all the submissions
  If previous_submission_date != current_submission_time            
  }
    Display current_submission_date
    previous_submission_date = current_submission_time 
  }
<% end %>
"You want stimulus? How about we cut off your balls!" -- Slavoj Zizek

Re: If Else Statement in Views

Use group_by, for example:

@submissions.group_by { |s| s.created_at.to_date }.each do |date, submissions|
  <h3><%= date %></h3>
  <% submissions.each do |submission| %>
    # submission data goes here
  <% end %>
<% end %>

Group by will give you an ordered hash where the key is the submission date and the values are arrays of submissions for that date.

Rob Anderton
TheWebFellas

Re: If Else Statement in Views

Thank you. That one seems very promising so far.
I have the following problem now though.

Instead of getting...
Date 1
Post 1
Post 2
Date 2
Post 3

I get...
Date 1
Date 2
Post 1
Post 2
Post 3
Post 1
Post 2
Post 3

I think this part of the code is being repearted by the number of different dates.

<% submissions.each do |submission| %>
    # submission data goes here
<% end %>
"You want stimulus? How about we cut off your balls!" -- Slavoj Zizek