I need to make a report and I have the following query :
Deducao.joins(:prestador)
.select('pessoas.razao_social, pessoas.qtd_min_mensal, count(deducoes.id)')
.group('deducoes.prestador_id')
.having('count(deducoes.id) < pessoas.qtd_min_mensal')
It generates the following SQL:
SELECT
pessoas.razao_social,
pessoas.qtd_min_mensal,
count(deducoes.id)
FROM
deducoes
INNER JOIN
pessoas ON pessoas.id = deducoes.prestador_id
GROUP BY
deducoes.prestador_id
HAVING
count(deducoes.id) < pessoas.qtd_min_mensal
The generated Sql is correct, and if executed in mysql returns as expected. But when running in activerecord it returns #<ActiveRecord::Relation [#<Deducao id: nil>, #<Deducao id: nil>]>
. Why does it return id: nil
instead of the fields I specified in select .select('pessoas.razao_social, pessoas.qtd_min_mensal, count(deducoes.id)')
?