2016-07-29 67 views
0

每當我嘗試執行find_with_reputationcount_with_reputation方法時,都會收到以下錯誤消息。Rails - ActiveRecord聲譽系統作用域查詢問題

ArgumentError: Evaluations of votes must have scope specified

我的模型定義如下:

class Post < ActiveRecord::Base 
    has_reputation :votes, 
      :source => :user, 
      :scopes => [:up, :down] 

的錯誤,當我試圖執行,例如提出了:

Post.find_with_reputation(:votes, :up) 

Post.find_with_reputation(:votes, :up, { order: "likes" }) 

不幸的是, documentation對於如何解決這個錯誤並不是很清楚。這只是說的方法如下,應執行:

ActiveRecord::Base.find_with_reputation(:reputation_name, :scope, :find_options) 

在車型沒有作用域與方法,如ActiveRecord的信譽系統運作良好:

User.find_with_reputation(:karma, :all) 

任何幫助將非常感激。

回答

0

我找到了解決方案。看起來ActiveRecord信譽系統加入rs_reputations表中的信譽和範圍名稱。因此,就我而言,:votes的範圍可能爲:up:down的名稱名稱分別命名爲:votes_up:votes_down

因此,需要這樣的待建find_with_reputationcount_with_reputation方法範圍的機型:

Post.find_with_reputation(:votes_up, :all, { conditions: ["votes_up > ?", 0] }) 

代替:

Post.find_with_reputation(:votes, :up, { conditions: ["votes_up > ?", 0] }) 

注意,您需要添加conditions選項來獲得所需的結果,否則它會帶來模型的所有記錄,而不是那些票數爲正數的記錄。