# Help speeding up this code?

**URL:** <https://www.ruby-forum.com/t/help-speeding-up-this-code/143995>\
**Category:** Rails\
**Created:** [July 24, 2008, 2:53pm UTC](https://www.ruby-forum.com/t/help-speeding-up-this-code/143995 "2008-07-24T14:53:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![amanda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/67e7ee/32.png) [@amanda](https://www.ruby-forum.com/u/amanda)\
**Post date:** [July 24, 2008, 2:53pm UTC](https://www.ruby-forum.com/t/help-speeding-up-this-code/143995/1 "2008-07-24T14:53:01Z")

</div>

Hi there, I am working on an events site where there are a bunch of  
different calendars (for different groups of people) that display  
submitted events that belong to that calendar, the dates for the events  
are kept as Occurrence objects.  
This code works fine, it just takes really long to load a page if I use  
it, it pulls events for a particular calendar from the database and  
displays the event info on the page. Any suggestions on how to speed it  
up? I’m pretty sure it takes a long time because it’s pulling all the  
occurrences (theres a lot of them) of all events from the database and  
then going through all of those and rejecting all but the few events  
that belong the necessary calendar.

CalendarController

@calendar = params[:cal]; # calendar for the events to belong to  
@length = params[:length];  
@limit = params[:limit]; # tells how many events are being called for

if @limit == nil  
@limit = ‘-1’;  
end

if @length == ‘all’  
@occurrences = @calendar.all\_events()  
end

Calendar model

def all\_events() #the part that I think takes forever to load  
o = Occurrence.find\_all\_events()

```
o.reject do |c|
  c.event.nil? || !c.event.calendars.include?(self)
end

```

end

Occurrence model

def find\_all\_events()  
Occurrence.find :all  
end

Thanks for any help!

---

<div class="post-metadata">

**Author:** ![amanda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/67e7ee/32.png) [@amanda](https://www.ruby-forum.com/u/amanda)\
**Post date:** [July 24, 2008, 5:38pm UTC](https://www.ruby-forum.com/t/help-speeding-up-this-code/143995/2 "2008-07-24T17:38:34Z")

</div>

Nevermind, sped it up by limiting the Occurrence.find :all to find only  
occurrences dated to start on the current date and after it

---

<div class="post-metadata">

**Author:** ![amanda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/67e7ee/32.png) [@amanda](https://www.ruby-forum.com/u/amanda)\
**Post date:** [July 24, 2008, 11:23pm UTC](https://www.ruby-forum.com/t/help-speeding-up-this-code/143995/3 "2008-07-24T23:23:50Z")

</div>

Amanda … wrote:

> Nevermind, sped it up by limiting the Occurrence.find :all to find only  
> occurrences dated to start on the current date and after it

Even though you’ve solved this problem, I suggest taking a look at your  
log to see what (and how many) SQL queries are being executed. I suspect  
you have at least one n+1 issue. There are a few ways to solve this –  
the easiest is probably to use the :include option.
