1
該查詢工作正常,但作爲一個練習,我將如何轉換它利用ActiveRecord方法?Rails:這個SQL查詢可以重寫爲使用ActiveRecord嗎?
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")
這裏是我的模型結構:
DisplayStat (id: integer,
banner_id: integer,
comments: string,
impressions: integer,
cost: integer,
created_at: datetime,
updated_at: datetime)
是否可以寫上面的查詢,而無需使用原始的SQL?
的運轉軌道3.1和MySQL 5.5