2013-09-30 4 views
6

द्वारा act_as_votable ऑर्डरिंग मैं acts_as_votable gem का उपयोग करके अपवॉट्स की संख्या के आधार पर प्रश्नों के क्रम में काम करने के लिए अब तक काम करने में सक्षम नहीं हूं।upvotes

यहाँ मेरी वोट दें और सूचकांक तरीके हैं:

def upvote 
    @question = Question.find params[:id] 
    @question.liked_by current_user 
    redirect_to comment_questions_path 
end 

def index 
@comment = Comment.find params[:comment_id] 
@questions = @comment.questions 
end 

और मेरे सवालों देखने:

<%= div_for(question) do %> 
<% if question.votes.size > 0 %> 
<div class="verifiedanswer"> 
<%= question.body %> 
</div> 

<% else %> 
<div class="answercontainer2"> 
<%= question.body %> 
</div> 
<% end %> 

क्या मैं देख सकते हैं और नियंत्रक यह काम करने के लिए रखना चाहिए?

उत्तर

11

इस विशेष मणि में एक कैशिंग माइग्रेशन है जिसे आप भी चला सकते हैं।

https://github.com/ryanto/acts_as_votable#caching

class AddCachedVotesToPosts < ActiveRecord::Migration 
    def self.up 
    add_column :posts, :cached_votes_total, :integer, :default => 0 
    add_column :posts, :cached_votes_score, :integer, :default => 0 
    add_column :posts, :cached_votes_up, :integer, :default => 0 
    add_column :posts, :cached_votes_down, :integer, :default => 0 
    add_index :posts, :cached_votes_total 
    add_index :posts, :cached_votes_score 
    add_index :posts, :cached_votes_up 
    add_index :posts, :cached_votes_down 

    # Uncomment this line to force caching of existing votes 
    # Post.find_each(&:update_cached_votes) 
    end 

    def self.down 
    remove_column :posts, :cached_votes_total 
    remove_column :posts, :cached_votes_score 
    remove_column :posts, :cached_votes_up 
    remove_column :posts, :cached_votes_down 
    end 
end 

मेरे सुझाव नमूना कोड के साथ एक नया माइग्रेशन बना सकते हैं और उपयोग करने वाले के खिलाफ सॉर्ट करने के लिए होगा।

एक बार जब आप बनाने के बाद प्रवास, आप उन स्तंभों में से एक पर सॉर्ट कर सकते हैं:

http://guides.rubyonrails.org/active_record_querying.html#ordering

उदाहरण के लिए:

<% Post.order(:cached_votes_up).each do |post| %> 
    ... html goodness here ... 
<% end %> 

कि वोट की संख्या से सॉर्ट होगा।

+0

क्षमा करें, रेल के लिए नया है और यह सुनिश्चित नहीं है कि मैं इसे अपने विचार में कैसे कार्यान्वित करूं। – user2759575

+0

थोड़ा और जोड़ा गया। एचटीएच –

+0

बहुत बढ़िया, धन्यवाद। – user2759575

संबंधित मुद्दे