Correctly handle joining scoped associations with table aliases.

Backport of #14154 to 4-0-stable.

/cc @abstractcoder - this fixes #12770 for 4.0.x
/cc @tenderlove