You say that you have a column named date
in your Purchase
model's table (that's a terrible name for column BTW, using type names for column names just leads to pain and suffering). Presumably your date
column is a date
or timestamp
. In your Purchase.find_by_sql
, you have this:
EXTRACT(MONTH FROM purchases.date) AS date
and Purchase.find_by_sql
will be trying to create Purchase
instances. So you're giving Purchase
a simple number and calling it date
, Purchase
probably notices that it has a date
column and tries to parse your simple number as a full date or timestamp. A simple number cannot be parsed as a date or timestamp in any meaningful fashion so they're probably all get converted to nil
.
You have two things to do here:
- Renamed that
date
column to something else, something that doesn't conflict with any keywords. And to hopefully fix your immediate problem, use a different alias for your month number:
EXTRACT(MONTH FROM purchases.date) AS month_number
That should get you a
month_number
method on the returnedPurchase
objects.