Самое простое:
Operation.find_by_sql [
'select
month(created_at) as "mês",
sum(points) as "total de pontos",
count(*) as "operações",
avg(points) as "média"
from cashmilhas_07_16.operations
where operation_type = :type and created_at >= :date
group by year(created_at), month(created_at)',
{ type: 1, date: '2018-01-01' }
]
Или:
Operation.where("operation_type = :type and created_at >= :date", { type: 1, date: '2018-01-01' })
.select('month(created_at) as "mês", sum(points) as "total de pontos", count(*) as "operações", avg(points) as "média"')
.group("year(created_at), month(created_at)")
Оба метода хорошо описаны в Rails API