Rails 用の Activerecord-reputation-system gem を使用しています。
Product
スコア(評判)でソートされたモデルからすべての製品を呼び出したいと思います。- また、評判を期間で制限する方法を教えてください。すべての製品を先月のみの投票で並べ替えたいとしましょう。
Rails 用の Activerecord-reputation-system gem を使用しています。
Product
スコア(評判)でソートされたモデルからすべての製品を呼び出したいと思います。もちろん、コレクションを手動で並べ替えることができます。
@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