Rails每个日期查询超过n个帖子

时间:2017-05-31 11:51:48

标签: sql ruby-on-rails activerecord orm

我有UserPost个型号has_many<=>belongs_to

我需要找到每天超过N个帖子的用户

我只做了这个查询,允许我找到帖子多于N

的用户
User.select('users.*, count(posts.id)').joins(:posts).group('posts.id').having('count(posts.id) > 1')

如何更新我的查询?

2 个答案:

答案 0 :(得分:0)

实现了

User.select('users.*, count(posts.id)').joins(:posts).
group('users.id').
having('count(posts.id) >= 1').
where("DATE(posts.created_at) = ?",Date.today)

答案 1 :(得分:0)

尝试

Write-SqlTableData : The term 'Write-SqlTableData' is not recognized as the name of a cmdlet, function, script file, 
or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and 
try again.

这里的区别在于按天分组,而不是按计数分组。