我使用其中一個用戶寫的評論和其他用戶進行表決的項目中的Thumbs_up寶石。我遇到了同樣的問題,並提出了下面的解決方案。它不是優雅的,但它的作品。我希望有經驗的人能提供更好的解決方案。
在我的用戶模型中,我有:
class User < ActiveRecord::Base
acts_as_voter
has_karma(:chapters, :as => :user) # tracks "up votes" for all a user's chapters
has_many :reviews
end
我的評論模式有:vote_cnt屬性和數據庫列,我更新每當投票投:
class Review < ActiveRecord::Base
attr_accessible :heading, :body, :notes, :published, :user_id, :vote_cnt
acts_as_voteable
belongs_to :user
###############################################################################
# update the vote_cnt field in the database, for sorting and query purposes.
def update_vote_count
if self.vote_count != self.vote_cnt
self.vote_cnt = self.vote_count
self.save
end
end
end
在我的評論控制器I在我的評論視圖中有處理點擊投票鏈接的操作:
################################################################
def vote_for
@review = Review.find(params[:id])
begin # try-rescue
current_user.vote_for(@review)
@review.update_vote_count
redirect_to review_path(@review), :notice => "Successfully voted for this review."
rescue
redirect_to review_path(@chapter), :notice => "Sorry, can't vote for a review twice, or you don't have voting rights."
end
end
現在,升,我終於可以檢索集評論由評論的投票下令CURRENT_USER訪問的:
@reviews [email protected]_by(current_ability).order("vote_cnt DESC")
就像我說的,不優雅,但它的作品。您可以按照我的評論構建您的微博,它應該適合您。希望這可以幫助你。
我真的很感謝你的幫助,明天我會試試這個,我會告訴你它是如何去的,再次感謝你! – Kellogs 2012-03-07 06:49:49