按票数对帖子进行排序

发布于 2024-11-15 13:16:24 字数 988 浏览 2 评论 0原文

大家好,老读者,第一次发帖:)

我有一个包含帖子的应用程序,我正在使用 gemthums_up (rails v. 3.0.7) 为帖子添加投票。我已经为显示前 10 名帖子的帖子实现了名人墙,但我只是不知道如何按投票对帖子进行排序(加减法)。

现在我只是使用:

def wall_of_fame
@posts = Post.tally(
  {   :at_least => 1,
      :at_most => 10000,
      :limit => 10,
      :order => 'vote_count desc'
  })

end

和耻辱之墙:

def wall_of_fame
@posts = Post.tally(
  {   :at_least => 1,
      :at_most => 10000,
      :limit => 10,
      :order => 'vote_count asc'
  })

end

但实际上我需要使用 plusminus 方法来排序帖子,而不仅仅是 vote_count ,因为它只是显示我写了一些帖子,而不是得票最多的帖子,或者至少是这样的:

:order => 'votes_for asc'

对于 wall_of_shame

:order => 'votes_against asc'

目前 votes_for 和 votes_against 只能在视图中使用,例如

@post.votes_for

如何在我的帖子控制器中使用它们才能使用它们在 :order => 中?

谢谢。

Hi everybody long time reader, first time poster :)

I have an app with posts and I am using the gem thumbs_up ( rails v. 3.0.7) to add votes to the posts. I have implemented a wall of fame for the posts showing the top 10 posts and I just can't figure out how to sort the posts by votes ( plusminus method).

Now I am just using:

def wall_of_fame
@posts = Post.tally(
  {   :at_least => 1,
      :at_most => 10000,
      :limit => 10,
      :order => 'vote_count desc'
  })

end

and for the wall of shame:

def wall_of_fame
@posts = Post.tally(
  {   :at_least => 1,
      :at_most => 10000,
      :limit => 10,
      :order => 'vote_count asc'
  })

end

but actually I need to order the posts using the plusminus method and not just vote_count, because it just shows me some posts, not the ones with the most up votes, or at least something like:

:order => 'votes_for asc'

and for the wall_of_shame

:order => 'votes_against asc'

Currently votes_for and votes_against can be used only in the views like

@post.votes_for

How can I use them in my posts controller to be able to use them in :order => ?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

杯别 2024-11-22 13:16:24

我也面临这个问题,但我从头开始构建了我的投票机制。我最终在视图中对我的帖子进行了排序。所以,我像这样重新排列它们:

<%= render @posts.sort_by { |post| post.votes.size }.reverse %>

这将我的帖子从得票最多到最少得票排序。

祝你好运!

@thatdankent

I faced this issue as well, but I built my voting machinery from scratch. I ended up sorting my posts in the views. So, I redered them like this:

<%= render @posts.sort_by { |post| post.votes.size }.reverse %>

This ordered my posts from the most votes to the least votes.

Good Luck!

@thatdankent

是伱的 2024-11-22 13:16:24

您只需订购 plusminus:

http://gistpages.com/2013/08/28/order_by_votes_using_the_thumbs_up_gem< /a>

Design.plusminus_tally.order('plusminus_tally DESC')

You just need to order plusminus:

http://gistpages.com/2013/08/28/order_by_votes_using_the_thumbs_up_gem

Design.plusminus_tally.order('plusminus_tally DESC')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文