Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7989985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:53:15+00:00 2026-06-04T12:53:15+00:00

I’m building a small app that takes things entered into an input field and

  • 0

I’m building a small app that takes things entered into an input field and displays them directly below that input field when entered.

My goal with this code is to separate those entries by date so that all things posted on June 1 are posted with one line break between them and the first entry of June 2 has 2 spaces between it and the entries from June 1.

This is my code and it’s not acting as planned but I can’t figure out why, I think it stems from line 3 and something I’m doing incorrectly. (Note: I’m aware this doesn’t account for changes in month or year yet. I’ll get to that once I figure out proper date spacing)

<% for i in (0..(@allLessons.count-1)) %>

<b><%= @date[i].created_at.strftime('%b %d')%></b><br/>

<% if @date[i].created_at.strftime('%d') == @date[i-1].created_at.strftime('%d') %>

<%= @date[i].created_at.strftime('%d') %> <br />

<% else %>

<%= @date[i].created_at.strftime('%d') %><br /><br />

<% end %>           
<% end %>

From the controller:

@allLessons = Lesson.all
@date = Lesson.find(:all, :order => 'created_at ASC')

Any help you could lend on this would be hugely appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T12:53:17+00:00Added an answer on June 4, 2026 at 12:53 pm

    Blocks and iterators are where it’s at.

    @allLessons = Lesson.order('created_at ASC')
    @dates = @allLessons.group_by { |lesson| lesson.created_at.beginning_of_day }.sort
    
    <% @dates.each do |date, lessons| %>
      <% lessons.each do |lesson| %>
        <b><%= lesson.created_at.strftime("%d") %></b><br />
      <% end %>
      <br />
    <% end %>
    

    Annotated

    First we get all of the lessons together. This is equivilant to find(:all, :order => ‘created_at ASC’), but I like this newer, compact syntax

    @allLessons = Lesson.order('created_at ASC')
    

    Then we group them all together into a hash where the key is the date and the value is an array of records that were created on that day. beginning_of_day converts a DateTime into a Date where the time is set to 00:00:00. So, 2012-05-25 18:00 becomes 2012-05-25 00:00:00. This is so we can group the dates themselves without the time getting in the way

    @dates = @allLessons.group_by { |lesson| lesson.created_at.beginning_of_day }.sort
    

    @dates is now a hash where the keys are dates and the values are arrays of lessons from that date. for example, { '2012-05-24 00:00:00' => [ lesson_1 ], 2012-05-25 00:00:00' => [ lesson_2, lesson_3 ]

    We then pass the hash into a block, where the key is the date, and the value is the array of lessons. This is saying, for each date…

    <% @dates.each do |date, lessons| %>
    

    Give me the lessons that belong to that date. And for each of those…

      <% lessons.each do |lesson| %>
    

    print out the date of the lesson

        <b><%= lesson.created_at.strftime("%d") %></b><br />
      <% end %>
    

    before moving on to the next date, print a <br />

      <br />
    <% end %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I am currently running into a problem where an element is coming back from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.