Ruby on Rails Sunday, January 30, 2011

Frederick Cheung wrote in post #978493:
> On Jan 30, 12:51am, "Ellicks M." <li...@ruby-forum.com> wrote:
>> I want to be able to loop over all the event_occurrences and access the
>> data of the parent event. At the moment I'm doing this via something
>> like:
>>
>> ObjectSpace._id2ref(@parent_event_object_id).title
>>
>> but this is sometimes returning RangeError(xxx is a recycled object).
>> Assumedly the parent objects are being garbage collected.
>>
>> Does anybody have any suggestions on how this could be done correctly?
>
> Why could events not have an array of the dates on which they occur
> (or indeed, why do occurrences need to exist ahead of time - it seems
> that Event could have a method that given a range of dates computes
> all the occurrences for that range)
>
> Fred

Hi Fred,

Mainly speed/performance but these 'events' are looped through all over
the show. I think it'd be impractical to write a method for each unique
set of criteria:

er = get_eventroster
the_date = "2010-12-31".to_date

er.allevents.select{|e| e.calendar_id == 20135 && e.amount >= 500.0 &&
e.calendar_date == the_date}.each{|e| balance += e.amount}

is far more flexible than, say:

er.get_events_by_calendar_id_and_amount_greater_than_and_date(20135,
500.0, the_date).each{|e| balance += e.amount}

I have already tried something like:

er.allevents.select{|e| e.calendar_id == 20135 && e.amount >= 500.0 &&
e.occurs_on(the_date)}.each{|e| balance += e.amount}

but these events are being looped through in many places throughout my
application and this slowed the creation of a 1-year forecast calendar
from 0.5 seconds (where there was an individual event for every
instance) to 5.5 seconds (where each event held an array of dates
representing its instances) and 50-60 seconds (where the event held a
single date and occurrences where calculated on the fly based on repeat
information)

Using functions to calculate the number of occurrences between two dates
(etc) slows it down but lightens the amount of data stored in session.
The purpose of this challenge was to satisfy both--keep it fast but
don't repeat all the data that each event contains.

Does anybody know how I could loop through every instance of an event
and call it's parent events methods?

--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment