JSONAPI-Resources / JSONAPI-Resources/jsonapi-resources

ActiveRecord `count` with a Relation#group returns a hash, not an integer, which break Pagination

Ouverte
#984 4 commentaires 4 réactions 1 personne assignée Voir sur GitHub

@lgebhardt y travaille déjà.

Depuis le 10/7/2020.

Status: Needs Failing Test Type: Bug
Langage dominant
Ruby
Étoiles
2.3k
Forks
546
Métriques de merge des PR
Aucune PR mergée en 30 j

Description

I came across this bug in a large Rails API my team is building. The issue is that ActiveRecord count method does not always return an integer. As explained in the docs:

If count is used with Relation#group, it returns a Hash whose keys represent the aggregated column, and the values are the respective amounts:

Person.group(:city).count
# => { 'Rome' => 5, 'Paris' => 3 }

This breaks the expectation made in the various Paginator classes that record_count will be an integer.

The simple way to fix this is to handle the Hash case with hash.values.sum. I am putting in this Issue, instead of a MR, because I am not certain where in the stack you would think this should live. It could live in the AR adapter for find_count, it could live in the Processor when page_options[:record_count] is created, or it could live in the Paginator class.

If you give me any guidance on how you would like to fix this bug, I will happily put in the appropriate MR.

stephen

Guide de contribution

Aucun guide de contribution indexé pour ce dépôt

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Évaluation

Cette issue n'a pas encore été évaluée.

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.