I am trying to make a query to get all the visits in a project and the count of each visit maintenance_reports and recognitions. For some reason, I am getting an incorrect count when I add the recognitions to the join.
@visits = @project.visits.left_outer_joins(:maintenance_reports)
      .distinct
      .select(
        'visits.*, COUNT(maintenance_reports.*) AS maintenance_reports_count'
      )
      .group('visits.id')
      .order('id asc')
I get
@visits.find(2).maintenance_reports_count => 6
    @visits = @project.visits.left_outer_joins(:maintenance_reports, :recognitions)
      .distinct
      .select(
        'visits.*, COUNT(maintenance_reports.*) AS maintenance_reports_count, COUNT(recognitions.*) AS recognitions_count'
      )
      .group('visits.id')
      .order('id asc')
@visits.find(2).maintenance_reports_count => 2
                        
The counts need to be gathered as sub-queries and those sub-queries joined with the visits.