I have a PostgreSQL query that sums values by month, extracting the month value from a date:
SELECT vendors.name AS vendor,
SUM(purchases.invoice_total) AS invoice_total,
EXTRACT(MONTH FROM purchases.date) AS date
FROM vendors, purchases
WHERE purchases.vendor_id = vendors.id
AND purchases.store_id = ?
AND purchases.purchase_department_id = ?
AND (purchases.date BETWEEN ? AND ?)
GROUP BY vendor, EXTRACT(MONTH FROM purchases.date)
ORDER BY vendor, date;
After executing the query directly in the DB console, I get something like this:
+------------------------------------+---------------+------+
| vendor | invoice_total | date |
+------------------------------------+---------------+------+
| Store 1 | 448.56 | 1 |
| Store 1 | 263.44 | 2 |
| Store 1 | 939.47 | 3 |
The problem comes when using Rails' find_by_sql function. The vendor and invoice_total values get returned correctly, but the date values get returned as nil.
#<Purchase date: nil, invoice_total: #<BigDecimal:85c75c8,'0.26344E3',18(18)>>
This is my call. I've even tried with hardcoded values and nothing, but it's like the Extract function won't execute in Rails:
<% @purchasesSQL = Purchase.find_by_sql(["SELECT vendors.name AS vendor, SUM(purchases.invoice_total) AS invoice_total,
EXTRACT(MONTH FROM purchases.date) AS date
FROM vendors, purchases
WHERE purchases.vendor_id = vendors.id
AND purchases.store_id = ?
AND purchases.purchase_department_id = ?
AND (purchases.date BETWEEN ? AND ?)
GROUP BY vendor, EXTRACT(MONTH FROM purchases.date)
ORDER BY vendor, date;", @store.id, purchase_department.id, @start, @end])
%>
I've already done some debugging and the values I'm passing are not nil nor wrong.
purchasestable look like?