Fixed AR::Relation#where edge case with Hash and other Relation

Backport #16502 to 4.1 stable

Example:

  Author.where(posts: { author_id: Author.where(country_id: 1) }).joins(:posts)