Skip to main content
título editado
Link
Adell
  • 131
  • 12

Active record, como substituir o uniq pelo distinct de forma correta?

Fonte Link
Adell
  • 131
  • 12

Active record, substituir o uniq pelo distinct

Estou tentando recuperar os meses onde tenho respostas a uma pesquisa desta maneira:

Answers.pluck('EXTRACT(MONTH FROM created_at)').uniq


(0.7ms)  SELECT EXTRACT(MONTH FROM created_at) FROM `answers`
=> [3]

Desta maneira tenho o resultado esperado, mas recebo um warning do rails:

DEPRECATION WARNING: uniq is deprecated and will be removed from Rails 5.1
(use distinct instead) (called from __pry__ at (pry):8)

Quando tento seguir a sugestão de usar o distinct sem parametros:

Answers.pluck('EXTRACT(MONTH FROM created_at)').distinct()
   (0.4ms)  SELECT EXTRACT(MONTH FROM created_at) FROM `answers`
NoMethodError: undefined method `distinct' for [3, 3]:Array
from (pry):33:in `__pry__'

com parâmetro:

Answers.pluck('EXTRACT(MONTH FROM created_at)').distinct('EXTRACT(MONTH FROM created_at)')
   (0.4ms)  SELECT EXTRACT(MONTH FROM created_at) FROM `answers`
NoMethodError: undefined method `distinct' for [3, 3]:Array
from (pry):33:in `__pry__'

Answers.pluck('EXTRACT(MONTH FROM created_at)').distinct(:created_at)
   (0.7ms)  SELECT EXTRACT(MONTH FROM created_at) FROM `answers`
NoMethodError: undefined method `distinct' for [3, 3]:Array
from (pry):35:in `__pry__'

A maneira de se usar é outra?