0

大家好,长期读者,第一次海报:)

我有一个带有帖子的应用程序,我正在使用 gem thumbs_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'
  })

结尾

和耻辱之墙:

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

结尾

但实际上我需要使用plusminus方法而不只是vote_count来排序帖子,因为它只是向我显示一些帖子,而不是投票最多的帖子,或者至少是类似的:

:order => 'votes_for asc'

并为 wall_of_shame

:order => 'votes_against asc'

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

@post.votes_for

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

谢谢你。

4

2 回答 2

2

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

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

这将我的帖子从最高票数到最低票数排序。

祝你好运!

@thatdankent

于 2011-08-06T10:21:47.203 回答
1

您只需要订购 plusminus:

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

Design.plusminus_tally.order('plusminus_tally DESC')
于 2013-08-28T12:17:28.097 回答