Consider a ‘families' table in a database with a ‘husband’ and ‘wife’ column. You add a composite key so that the combination husband and wife is unique. You don’t want a second record with the same husband and wife combination. At the database level your migration could look like:
Validations in Rails 2
Now you want to add a corresponding validation in the ‘family’ model. In Ruby on Rails 2 we can use a scope:
Validations in Rails 3
In Rails 3 this looks slightly different:
I wouldn’t have found this without the help of KandadaBoggu and epochwolf on stackoverflow.