0

我目前有这样的工作:

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_atperiod还是period_registration

4

1 回答 1

3

试试这个查询

PeriodRegistration.joins(:period).where("period_registrations.created_at >= ?", 30.days.ago.to_date).group("periods.product_id").count

这将返回每个 product_id 中有多少对象的哈希

于 2012-08-24T04:13:20.350 回答