ActiveRecord and/or Ruby Date help requested

I have model called Slot, which has a start_on and end_on, both of which
are dates.

A Team has_many Slots.

The two things I’m looking to do are:

  1. Ensure that newly created slots dont overlap any existing ones for a
    particular team.
  2. Quickly retrieve the ‘current’ slot given a Team and Time.now.

I can figure out how to do these two operations with a bit of grunting
and sweaty brute force, but I’m wondering if anyone with a bit more
ActiveRecord-fu can help me out with any shortcuts to something more
beautiful. :slight_smile:

Any help appreciated.

A.

Not sure these are any better than your brute force, but…

  1. Ensure that newly created slots dont overlap any existing ones for a
    particular team.

In your model define a ‘before_save’ method that queries the database to
find any slots that overlap the one you are trying to insert and if you
find them, return false which will cause the save to fail.

  1. Quickly retrieve the ‘current’ slot given a Team and Time.now.

Slot.find(:first,
:conditions => ["? BETWEEN start_on AND end_on AND team_id = ?",
Time.now, Team.id])