查询所有用户所选月份所有月份的已创建文章

时间:2016-04-25 09:54:15

标签: sql ruby-on-rails postgresql activerecord

我坚持这个,  我有一个文章表,其中包含updated_at和updated_by。我需要编写查询,这将为我提供所选年份所有月份的每位用户更新文章的数量。

谢谢

2 个答案:

答案 0 :(得分:0)

试试这个:

在这里,我将2016年作为年份并假设您已current_user可供

current_user.articles.where("extract(year from updated_at)= ?", 2016).count

答案 1 :(得分:0)

嘿,你可以这样试试:

Article.group("updated_by, date_trunc('month', updated_at)").where("extract(year from updated_at)= ?", 2016).count

它会返回[updated_by, Month_NO] => count

之类的结果