# Best way to populate a calendar

**URL:** <https://www.ruby-forum.com/t/best-way-to-populate-a-calendar/82963>\
**Category:** Rails\
**Created:** [January 22, 2007, 7:02pm UTC](https://www.ruby-forum.com/t/best-way-to-populate-a-calendar/82963 "2007-01-22T19:02:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![joshs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e5b9ba/32.png) [@joshs](https://www.ruby-forum.com/u/joshs)\
**Post date:** [January 22, 2007, 7:02pm UTC](https://www.ruby-forum.com/t/best-way-to-populate-a-calendar/82963/1 "2007-01-22T19:02:11Z")

</div>

Hi All,

I’m looking for advice on the best way to populate a calendar.  
Currently, I can think of two ways to do it:

1. 

Loop through each day on my calendar and request all items for that  
day from the database in each iteration…which would be thirty or so  
db queries per page load.  
\*\* this way is obviously quite db heavy  
2)  
Make one request for all events in the month and then loop through  
each day on my calendar and then loop through all my results to find a  
match.  
\*\* this way is quite ruby / processing heavy

Which is better?

---

<div class="post-metadata">

**Author:** ![joshs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e5b9ba/32.png) [@joshs](https://www.ruby-forum.com/u/joshs)\
**Post date:** [January 22, 2007, 7:08pm UTC](https://www.ruby-forum.com/t/best-way-to-populate-a-calendar/82963/2 "2007-01-22T19:08:02Z")

</div>

Josh S. wrote:

> Hi All,
> 
> I’m looking for advice on the best way to populate a calendar.  
> Currently, I can think of two ways to do it:
> 
> 1. 
> 
> Loop through each day on my calendar and request all items for that  
> day from the database in each iteration…which would be thirty or so  
> db queries per page load.  
> \*\* this way is obviously quite db heavy  
> 2)  
> Make one request for all events in the month and then loop through  
> each day on my calendar and then loop through all my results to find a  
> match.  
> \*\* this way is quite ruby / processing heavy
> 
> Which is better?

You should be able to order the results from #2 in such a manner that  
each day comes out in sequence… thereby causing only one run through  
the entire returned results, adding each event to each day as you reach  
it.

---

<div class="post-metadata">

**Author:** ![joshs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e5b9ba/32.png) [@joshs](https://www.ruby-forum.com/u/joshs)\
**Post date:** [January 22, 2007, 7:21pm UTC](https://www.ruby-forum.com/t/best-way-to-populate-a-calendar/82963/3 "2007-01-22T19:21:14Z")

</div>

Hello Josh,

2007/1/22, Josh S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 1. 
> 
> Make one request for all events in the month and then loop through  
> each day on my calendar and then loop through all my results to find a  
> match.  
> \*\* this way is quite ruby / processing heavy

This is also the way I went. I order the results by event date, then  
I use ActiveSupport’s Enumerable #group\_by to get a Hash for each  
date.

Then, it’s a simple matter to run through each date:

# controller

@events = Event.find(:all, :conditions =\> [‘date BETWEEN ? AND ?’,  
@start\_date, @end\_date], :order =\> ‘date’).group\_by(&:date)

# @events is a Hash where the key is the event’s date, and the value

is an Array of Event objects

# view or helper

(@start\_date … @end\_date).each do |date|  
render\_events\_for\_date(date, @events[date]) # @events[date] is  
possibly nil  
end

## Hope that helps !

François Beausoleil  
[http://blog.teksol.info/](http://blog.teksol.info/)  
[http://piston.rubyforge.org/](http://piston.rubyforge.org/)

---

<div class="post-metadata">

**Author:** ![joshs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e5b9ba/32.png) [@joshs](https://www.ruby-forum.com/u/joshs)\
**Post date:** [January 22, 2007, 7:29pm UTC](https://www.ruby-forum.com/t/best-way-to-populate-a-calendar/82963/4 "2007-01-22T19:29:16Z")

</div>

Awesome! Thanks to both of you for your help.

-josh
