我接近Ruby on Rails的新手(但喜歡它),我無法管理過濾acuratelly。act_as_votable rails計算特定用戶的答案投票
Mi的目標是檢索由一個用戶創建的答案上的所有投票。而我使用act_as_votable_gem
在answer.rb我:
class Answer < ActiveRecord::Base
acts_as_votable
belongs_to :user
belongs_to :letter
...
end
在user.rb我:
class User < ActiveRecord::Base
acts_as_voter
has_many :answers, dependent: :destroy
...
end
在users_controller.rb談到的伎倆,因爲我有信形式用戶show動作:
def show
@user = User.find(params[:id])
@letter = Letter.new(params[:letter])
@letters = @user.letters.all
@answers = @user.answers
...
@emitedupvotes = @user.votes.up.count
@emiteddownvotes = @user.votes.down.count
@totalemitedvotes = @emitedupvotes + @emiteddownvotes
@receivedupvotes = ????????
@receiveddownvotes = ???????
...
end
我已經tryed:
a)(recomended one)@receivedupvotes = @ answer.votes.up.count但這需要@answer = Answer.find(params [:id]),我有=>「ActiveRecord :: RecordNotFound at/users/1找不到應答,id = 1「
或者,如果我這樣做:Answer.find(params [:answer_id])=>」無法找到答案沒有ID「。
B)(最合乎邏輯的)@receivedupvotes = @ answers.votes.up.count => 「未定義的方法`票的零:NilClass」
C)(瘋狂的一個)@receivedupvotes = @answers .user.votes.up.count =>「undefined method`user'」
d)@receivedupvotes = @ answers.votes.up.where(「voter = @user」)。count =>「未定義的方法`投票'爲零:NilClass「。
所以,我tryed連接(以前從未做過):
E)@receivedupvotes = @ answers.joins(:票).votes.up.count =>「未定義的方法'票的... 「
F)@receivedupvotes = @ answers.joins(:票).up.count => 」爲......「
任何幫助未定義的方法'了」?謝謝。