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 7042985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:13:57+00:00 2026-05-28T02:13:57+00:00

I have a model called event_recurrences it contains 2 important columns event_id and cached_schedule

  • 0

I have a model called event_recurrences it contains 2 important columns event_id and cached_schedule

cached_schedule contains an array of dates

event_id is used to reference the event

I need to

  1. Get all the event_recurrence objects @something.event_recurrences – Done

  2. Go through each recurrence object and get the event_id and all the
    dates from cached_schedule

  3. Iterate through each month, and spit out a list like the following

Jan

event_id date

event_id date

event_id date

Feb

event_id date

… and so on

To recap the event_id is located event_recurrence.event_id the dates that the event_id will happen on are located in an array inside event_recurrence.cached_schedule

Some I have some incomplete code to work with…

This code works successfully to show each event_recurrence object by month using the created_at field.

in my controller

@schedule_months = @something.event_recurrences.order("created_at DESC").group_by { |e| e.created_at.beginning_of_month }

in my view

<% @schedule_months.keys.sort.each do |month| %>  
    <div class="month">
        <%= month.strftime("%B %Y") %>
    </div> 
  <% for event in @schedule_months[month] %>  
    <li><%= event %></li>
  <% end %>  
<% end %>

Thanks in advance.

Sincerely, jBeas

  • 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-05-28T02:13:57+00:00Added an answer on May 28, 2026 at 2:13 am

    There are some details missing from your question… for example, can the dates in cached_schedule span multiple months, or are they all guaranteed to be in the same month?

    If you just want to use core Ruby:

    dates = []
    @something.event_recurrences.each do |er| 
      er.cached_schedule.each { |date| dates << [date, er.event_id] }
    end
    @event_dates = dates.group_by { |(date,event_id)| date.mon }
    

    Then in the view:

    <% @event_dates.keys.sort.each do |month| %>
      <div class="month"> <%= month.strftime("%B %Y") %></div>
       <% @event_dates[month].each do |(date,event_id)| %>
          <li><%= date %> <%= event_id %></li>
       <% end %>
    <% end %>
    

    You may have to adjust the code a little depending on the specifics, but this should give you the idea. Note the use of destructuring assignment: @event_dates[month].each do |(date,event_id)|. This saves a line of code and expresses what the code is doing more clearly.

    If you don’t mind adding your own extensions to the core Ruby classes, you could make this code even cleaner and more consise. I often use a method which I call mappend:

    module Enumerable
      def mappend
        result = []
        each { |a| enum = yield a; enum.each { |b| result << b } if enum }
        result
      end
    end
    

    The name is a mix of map and append — it is like map, but it expects the return value of the mapping block to also be Enumerable, and it “appends” all the returned Enumerables into a single Array. With this, you could write:

    @event_dates = @something.event_recurrences.mappend { |er| er.cached_schedule.map { |date| [date, er.event_id] }}.group_by { |(date,event_id)| date.mon }
    

    OK, that might be a lot for one line, but you get the idea: it saves you from using an intermediate variable to accumulate results.

    UPDATE: Something like mappend is now part of the Ruby core library! It’s called flat_map.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an Event model with a finish_time field and a form checkbox called
I have a Django model called Event that has a date field for the
I have a model called Contact which has_one guest like so. class Contact <
I have a model called Answer which has a ForeignKey relationship to another model
I have a model called Details, and two controller methods new and create. New
I have a model called company that has_many users then users belongs_to company. class
I have a model called user and another model called student. Neither of them
I have a model called Customer in google app engine with python: class Customer(db.Model):
Say I have a model called User that has the following parameters: favorite_color, favorite_animal,
Let's assume I have a model called product. Let's assume that product has three

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.