【问题标题】:Activerecord-reputation-system Rails gem how to sort by votes and recency of vote?Activerecord-reputation-system Rails gem 如何按投票和投票的新近度排序?
【发布时间】:2013-05-19 12:46:47
【问题描述】:

我正在为 Rails 使用 Activerecord-reputation-system gem。

  1. 我想调用Product 模型中的所有产品,按它们的分数(声誉)排序。
  2. 您能否告诉我如何按时间段限制声誉 - 假设我只想按上个月发生的投票对所有产品进行排序。

【问题讨论】:

    标签: ruby-on-rails rubygems


    【解决方案1】:

    当然,您可以手动对集合进行排序:

    @products.sort! {|p1, p2| p1.reputation_for(:votes) <=> p2.reputation_for(:votes)}
    

    就个人而言,我更喜欢范围/连接驱动的方式来执行此操作,以允许操作数保持 ActiveRecord::Association。

    Products.find_with_reputation(:votes, :all, {:order => 'votes DESC'})
    

    或使用范围:

    def self.most_voted
      find_with_reputation(:votes, :all, {:order => 'votes DESC'})
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-25
      • 1970-01-01
      相关资源
      最近更新 更多