Join Table Validation Question

Hi All,

Here's my situation. People have Availability for Events. I need to ensure that each Person is Available for each Event exactly once.

I'm thinking the way to do it would be with a before_save method in Availability that does an

Availability.find("event_id = ? and person_id = ?", record.event_id, record.person_id)

and complains if it finds one.

Is this the way to go or is there a better approach?

**Leigh

Hi All,

Here's my situation. People have Availability for Events. I need to ensure that each Person is Available for each Event exactly once.

I'm thinking the way to do it would be with a before_save method in Availability that does an

Availability.find("event_id = ? and person_id = ?", record.event_id, record.person_id)

and complains if it finds one.

Is this the way to go or is there a better approach?

I'd stick a unique index on that pair of columns too - it's the only
way to get a cast iron guarantee for this sort of thing.

Fred

Hi All,

Here's my situation. People have Availability for Events. I need to

ensure that each Person is Available for each Event exactly once.

I'm thinking the way to do it would be with a before_save method in

Availability that does an

Availability.find("event_id = ? and person_id = ?", record.event_id,

record.person_id)

and complains if it finds one.

Is this the way to go or is there a better approach?

I'd stick a unique index on that pair of columns too - it's the only
way to get a cast iron guarantee for this sort of thing.

Is this what you meant, Fred?

      add_index "availabilities", ["person_id", "event_id"],
        :name => "person_id_event_id", :unique => true

**Leigh

That's the one.

Fred