ActiveRecord count fails when run with multiple includes and a join

I would appreciate some eyes on a patch I created for an error that occurs when you run a count calculation with multiple includes and at least one join. JoinDependency#graft does not set the parent join to the base join when a suitable parent is not found in the join dependency which causes an error when looking up the association to include.

The lighthouse ticket and patch can be found at