Topic: Modeling a recurring event

What is the best way to model an event that happens on a recurring basis? e.g. once a month, once a week, every thursday, etc. I know chronic can parse natural text but how do you make it recurring I can think of setting a date and then expiring it when the event passes, but that seems bad. I could also see testing to see if its a wednesday, but once again that doesn't seem like a good solution. Any ideas?

http://www.brianthecoder.com

Rails, Javascript, Actionscript and More.

Re: Modeling a recurring event

Good question, this is a tough one.

You need to first figure out the user interface. How do you plan to display the recurring event and what user input fields will you be accepting to set the recurring event?

Once you figure that out you basically have two options:

1. Store just the information the user submitted in the database. Perhaps you can do this with a "Recurring" model that is related to an "Event" model. This is DRY because the information is just kept in one spot. However, this will make it harder to display the events because you need to constantly check for recurring events and perhaps generate the Event models for each recurring instance dynamically.

2. Upon submitting the recurring information, generate many Event records based off of that information. This is not DRY because it duplicates the information all over the database. However, it will be easier to fetch the events if you are displaying it in a month view or something.

The first approach is far better when it comes to maintenance and keeping everything clean. Since it is DRY it will be easy to go back and alter the behavior of the recurring event - you will only need to change it in one spot in the database. It also allows you to do an infinite recurring event that never has an end date.

Railscasts - Free Ruby on Rails Screencasts