Uniqueness validations for nested associations.

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