Uniqueness validations for nested associations. Fixes #4568.

This is a condensed and simplified version of @jeyb #9364 and @wangjohn's #8308.

I've tried to address the comments in those other threads.

@nertzy, any comment?