2013-09-30 8 views
6

Şu ana kadar acts_as_votable gem numarasını kullanarak soruları cevaplayarak soru sormak için şimdiye kadar işe yarayan hiçbir şey bulamadık. İşte acts_as_votable ordering byvote

benim upvote ve dizin yöntemleri şunlardır:

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 

ve sorularım bakın:

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

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

Ben görünümü ve bu işi yapmak için denetleyici neler koymalıyım?

cevap

11

Bu özel mücevherde de çalıştırabileceğiniz bir önbellek göç vardır.

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 

Benim önerim örnek koduyla yeni göç oluşturmak ve karşı sıralamak için kullanabilirsiniz olacaktır.

Eğer göç, sen bu sütunların birinin üzerinde sıralayabilir o oluşturduktan sonra: Örneğin

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

: oyların kadar sayısına göre sıralanır

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

.

+0

Üzgünüm, yeni raylara ve bunu nasıl uygulayacağımı bilmiyorum. – user2759575

+0

Biraz daha eklendi. HTH –

+0

Harika, teşekkürler. – user2759575

İlgili konular