gpt4 book ai didi

ruby-on-rails - 获取评论最多的对象

转载 作者:行者123 更新时间:2023-12-04 23:59:53 25 4
gpt4 key购买 nike

我正在使用 acts_as_commentable_with_threading gem 让用户能够评论我的博客文章。

我现在想做的是显示评论最多的帖子,但我不知道如何查询它们(据我所知,gem 没有提供这种方法)。你能给我写一些技巧或想法如何实现这样的目标吗?

最佳答案

这是我用来返回发布最多项目的顶级用户的方法。它可以帮助您解决您的问题。我把它放在 Application Helper 中,因为它是我的侧边导航栏的一部分,将在 Web 应用程序的每个页面上使用。

def top_posters
User.all(:select => "users.*, COUNT(user_id) as post_count",
:joins => "LEFT JOIN posts AS posts ON posts.user_id = users.id",
:group => "posts.user_id",
:order => "post_count DESC",
:limit => 5)
end

在我看来,我有
<% top = top_posters() %>
<% for t in top %>
<li><%= link_to t.username, user_path(t) %>
(<%= t.posts.public_posts.count %>)</li>
<% end %>

关于ruby-on-rails - 获取评论最多的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11452215/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com