help speeding up this code?

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!

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

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.