[Ruby] Creating a small calendar system..

Scott Windsor swindsor at gmail.com
Fri Jul 11 07:56:33 PDT 2008


Domain modeling calender events can be pain.

Generally, based on the recurring type (weekly, monthly, one-time), you'll
need to store different fields for each.   For example, day_of_week
(smallint), day_of_month (smallint), recurring_start_date (date), etc, etc.
I'd probably recommend separating the recurrence model from your event
model, as well as seperating each recurring type.

For querying what events are going to happen on what day, it really varies
on your use cases.  If you only need to display when the next occurance of
that even is, you can keep a next_event_date and update it regularlly with a
cron job as the time passes.

But, if you want to display all of the events on a given day out in the
future, based on time, you're likely going to have to pull all of the events
based on their occurrance (i.e., it's a friday, so pull all events that
happen on friday, it's the 1st, so pull all the events that happen on the
first, etc).  It's more complex, but it beats the alternative of
pre-calculating out the event dates to make the query simple - that's just
going to end up using a ton of storage, with lots of bad gotachas down the
road.

If you really don't like having to query each of these seperately, the only
other option is to roll this into a SQL function and encapsulate the logic,
returning rows from your event table.  That might be the best way to keep
this performing fast and keep a lot of nasty SQL out of your finder code.

Also, be aware of time-based gotchas.  Day Light Savings time, recurring
events that happen at the 31st of the month, handling timezones, can all
cause really weird and bad effects when modeling calendars.  Just keep them
in mind and come up with a consistent way of handling them.

- scott

On Fri, Jul 11, 2008 at 1:15 AM, Joe Van Dyk <joe at pinkpucker.net> wrote:

> Hi,
>
> Using ActiveRecord, how would you setup the domain model for a system
> that models calendar events?  Events have the option of recurring
> every week or every month.  Need to be able to query what events are
> going to happen on what day.
>
> I'm sorta scratching my head over this, but I'm sure it's simple.
> Anyone want to get me on the right track?
>
> Thanks,
> Joe
> _______________________________________________
> Ruby at zenspider.com - Seattle.rb non-commercial list
> http://www.zenspider.com/seattle.rb
> http://www.zenspider.com/mailman/listinfo/ruby
>



More information about the Ruby mailing list