我目前有类似的工作:
period_registration = PeriodRegistration.count(:conditions => ["created_at >= ?", 30.days.ago.to_date], group: "date(created_at)")
但是,我想做这样的事情:
period_registration_product = PeriodRegistration.count(:conditions => ["created_at >= ?", 30.days.ago.to_date], group: "period_id.product")
当我这样做时,我在period_registration_product中得不到任何回复。在Period_Registration模型中有product_id时,按产品排序的最佳方法是什么?
更新
@period_registration_product = PeriodRegistration.joins(:period).where("date(created_at) >= ?", 30.days.ago.to_date).group("periods.product_id").count
结果:
SQLite3::SQLException: ambiguous column name: created_at: SELECT COUNT(*) AS count_all, periods.product_id AS periods_product_id FROM "period_registrations" INNER JOIN "periods" ON "periods"."id" = "period_registrations"."period_id" WHERE (date(created_at) >= '2012-07-25') GROUP BY periods.product_id
我是否需要指定哪个created_at,我想查看period
或period_registration
?
答案 0 :(得分:3)
尝试此查询
PeriodRegistration.joins(:period).where("period_registrations.created_at >= ?", 30.days.ago.to_date).group("periods.product_id").count
这将返回每个product_id中有多少个对象的哈希