JSONAPI-Resources / JSONAPI-Resources/jsonapi-resources

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

Aperta
#984 4 commenti 4 reazioni 1 assegnatario Vedi su GitHub

@lgebhardt ci sta già lavorando.

Dal 10/7/2020.

Status: Needs Failing Test Type: Bug
Lingua principale
Ruby
Stelle
2.3k
Fork
546
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

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

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.