My User model is pretty standard – it has an email:string column and I validate the uniqueness of this in the User model with validates :email, :unique => true.
In order to allow alternate email addresses for users, I created a new model:
AlternateAddress, with columns user_id:integer and email:string.
A User has_many AlternateAddresses, and an AlternateAddress belongs to a User.
This setup makes for a simple nested form, like this: http://railscasts.com/episodes/196-nested-model-form-part-1.
I realized that I need to validate the 2 email:string columns (one in User.rb, the other in AlternateAddress.rb) “together” – so there are no duplicate email addresses anywhere.
How would I do this? Or, is my entire methodology off?
Thanks in advance.
You can add a custom validator so that it checks the user table and then the alternate addresses table:
You can add something similar to the AlternateAddress field. The method is a custom validator to take care of alternate addresses, but you still want the normal rails validator for uniqueness amongst users.