class ActiveRecord::Associations::JoinDependency

def walk(left, right, join_type)

def walk(left, right, join_type)
  intersection, missing = right.children.map { |node1|
    [left.children.find { |node2| node1.match? node2 }, node1]
  }.partition(&:first)
  joins = intersection.flat_map { |l, r| r.table = l.table; walk(l, r, join_type) }
  joins.concat missing.flat_map { |_, n| make_constraints(left, n, join_type) }
end