I have a scenario where a doctor can have one or more specialties.
For each doctor, one and only one of her specialties can be designated
So I have tables called doctors, specialties, and doctors_specialties,
the last of which has a boolean is_primary column.
The doctor model class specifies that:
I want to enforce, at the lowest possible level, the constraint that
a doctor can only have one primary specialty.
So, whenever a member of the association is added or updated with the
value is_primary set to true, code is run to first set all records in
the association to primary = false, thus ensuring that the row about to
be saved is the only one with is_primary = true.
I looked into passing a code block to the HABTM, but couldn’t quite get
that to work. Anyone have any suggestions?
This is for a demo to show how one would do this in Rails as opposed to
existing code that does it in .NET. So I’d like the solution to be very
elegant and Rails-esque. Can someone help?