Suppose you have a ‘families’ table in your database with a ‘husband’ and ‘wife’ column. You add a a composite key so that the combination husband and wife is unique. You certainly don't want another record with the same husband and wife combination. At the database level your migration could look like this:
Now you want to add a corresponding validation in your family model. In Rails 2.x we can use a scope:
In Rails 3 this looks slightly different:
I wouldn't have found this without the help of KandadaBoggu and epochwolf on the stackoverflow forum, thanks guys!
This post is open source. Did you spot a mistake? Ideas for improvements? Contribute to this post via Github. Thank you!