The query works fine but as an exercise how would I convert it utilize ActiveRecord methods?
Model.find_by_sql("SELECT *, SUM(impressions) AS impressions, SUM(cost) AS cost
FROM display_stats
WHERE (created_at >= '#{(Date.today-2).to_time.strftime('%Y-%m-%d')}'
AND created_at <= '#{(Date.today-1).to_time.strftime('%Y-%m-%d')}')
GROUP BY banner_id
ORDER BY cost DESC")
Here is my model structure:
DisplayStat (id: integer,
banner_id: integer,
comments: string,
impressions: integer,
cost: integer,
created_at: datetime,
updated_at: datetime)
Is it possible to write the above query without using raw SQL?
Running Rails 3.1 and MySQL 5.5
Try this:
The date range is from the start to the end of the day; 2 days ago. When you pass a date range to the conditions hash, rails generates a
BETWEENoperator.