Rails activerecord:sum,max和join

Lud*_*vic 4 mysql activerecord ruby-on-rails

我有两个型号usersposts.用户可以投票和查看帖子

#users
  id
  name

#posts
  id
  count_votes
  count_views
  users_id
  created_at
  updated_at
Run Code Online (Sandbox Code Playgroud)

我希望过去24小时内收到最多票数和观点的用户.最大的观点和投票总和获胜.

我做了什么我 有这个SQL查询,它很好,但我想让用户拥有最多的投票,这个给我所有用户,我不知道如何添加count_views

select u.name as "Name", sum(p.count_votes)
from posts p 
inner join users u on p.user_id = u.id 
where p.created_at >= DATE_SUB(NOW(), INTERVAL 1 day)
group by user_id;
Run Code Online (Sandbox Code Playgroud)

ActiveRecord版本

Post.select("users.id, sum(posts.count_votes) as nb_votes")
.joins(:user).where("posts.created_at >= ?", 1.day.ago.beginning_of_day)
.group("posts.user_id")

# Results on IRB
=> #<ActiveRecord::Relation [#<Post id: 1>, #<Post id: 3>]> 
Run Code Online (Sandbox Code Playgroud)

如何在这两个总和上组合总和和最大值?有没有办法拥有activerecord代码或只有原始SQL?

Len*_*ran 5

您当前的查询对用户进行分组.因此,您将在输出中为每个用户获取一条记录.通过将输出限制为仅1个记录并按投票+视图总计数排序,您可以获得最高用户.

原始SQL:

select u.id, sum(p.count_votes + p.count_views) total
from posts p 
inner join users u on p.user_id = u.id 
where p.created_at >= DATE_SUB(NOW(), INTERVAL 1 day)
group by u.id
order by total DESC
limit 1 ;
Run Code Online (Sandbox Code Playgroud)

ActiveRecord版本:从用户模型开始,因此您将在输出中获取用户对象而不是Post对象,就像您在问题中提到的那样.

User.select("users.id, sum(posts.count_votes + posts.count_views) as nb_votes")
.joins(:post).where("posts.created_at >= ?", 1.day.ago.beginning_of_day)
.group("posts.user_id").order("nb_votes DESC").limit(1)
Run Code Online (Sandbox Code Playgroud)