Various refactoring to association scopes and joins
#28276

tenderlove wants to merge pkwip into master

We do not currently have an analysis of these two commits