2016-03-22 26 views
0

私は投稿のリストを持っており、それらのすべてが投票可能です。私は各投稿の投票数を数えることができますが、それらのすべての数をどのように数えることができますか?私はこのようなポストの数を数える投票システムRails:投稿一覧からの総投票数(acts_as_votable)

のための宝石acts_as_votableを使用しています:<%= performance_indicator.improvement_actions.count %>

これが私の「記事」のコントローラである:

class ImprovementActionsController < ApplicationController 
    before_action :set_improvement_action, only: [:show, :edit, :update, :destroy, :upvote, :downvote] 
    before_action :authenticate_user!, except: [:index, :show] 

    # GET /improvement_actions 
    # GET /improvement_actions.json 
    def index 
    end 

    # GET /improvement_actions/1 
    # GET /improvement_actions/1.json 

    def show 
    end 

    # GET /improvement_actions/new 
    def new 
    @performance_indicator = PerformanceIndicator.find(params[:performance_indicator_id]) 
    @improvement_action = ImprovementAction.new 
    @comment = @improvement_action.comments.new 
    end 

    # GET /improvement_actions/1/edit 
    def edit 
    end 

    # POST /improvement_actions 
    # POST /improvement_actions.json 
    def create 

    @performance_indicator = PerformanceIndicator.find(params[:performance_indicator_id]) 
    @improvement_action = @performance_indicator.improvement_actions.create(params[:improvement_action].permit(:description)) 
    @improvement_action.user_id = current_user.id if current_user 
    @improvement_action.save 


    respond_to do |format| 
     if @improvement_action.save 
     format.html { redirect_to @performance_indicator } 
     format.json { render :show, status: :created, location: @improvement_action } 
     else 
     format.html { render :new } 
     format.json { render json: @improvement_action.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # PATCH/PUT /improvement_actions/1 
    # PATCH/PUT /improvement_actions/1.json 

    def update 
    respond_to do |format| 
     if @improvement_action.update(improvement_action_params) 
     format.html { redirect_to performance_indicator_path(@improvement_action.performance_indicator), notice: 'Improvement action was successfully updated.' } 
     format.json { render :show, status: :ok, location: @performance_indicator } 
     else 
     format.html { render :edit } 
     format.json { render json: @improvement_action.errors, status: :unprocessable_entity } 
     end 
    end 
    end 


    def destroy 
    @improvement_action.destroy 
    respond_to do |format| 
     format.html { redirect_to performance_indicator_path(@improvement_action.performance_indicator), notice: 'Improvement action was successfully deleted.' } 
     format.json { head :no_content } 
    end 
    end 

    #upvote_from user 
    #downvote_from user 
    def upvote 
    @improvement_action.upvote_from current_user 
    # respond_to do |format| 
#  format.html { redirect_to :back } 
    # format.js { render layout: false } 
    # end 
    redirect_to :back 
    end 

    def downvote 
    @improvement_action.downvote_from current_user 
    redirect_to :back 
    ##respond_to do |format| 
    # format.html { redirect_to :back } 
    # format.js { render layout: false } 
    # end 
    end 


    private 
    # Use callbacks to share common setup or constraints between actions. 
    def set_improvement_action 
     @improvement_action = ImprovementAction.find(params[:id]) 
    end 

    # Never trust parameters from the scary internet, only allow the white list through. 
    def improvement_action_params 
     params.require(:improvement_action).permit(:description, :upvote, :downvote, :score, :active) 
    end 

    end 

そして、私はここに入れたいですカウンタ:

<% @performance_indicators.each do |performance_indicator| %> 
<p> Number of votes </p> 
<% end %> 
+0

http://apidock.com/ruby/Enumerable/inject –

答えて

0

ImprovementActionモデルの投票用のキャッシュ列はありますか? (https://github.com/ryanto/acts_as_votable#caching

各投稿の投票総数を維持するためです。

# in this case the cache column is :cached_votes_total 
sum = performance_indicator.improvement_actions.sum(:cached_votes_total) 

これにより、1つのデータベース要求のみが実行されます。

は次のように行うことはありません:これはロードし、すべてのレコードをインスタンス化し、それらのそれぞれが自分の票を取得するための個別の要求を行う必要があります

# DON'T DO THIS !!! 
performance_indicator.improvement_actions.inject(0) {|sum, post| sum + post.votes_for.size } 

。非常に悪い解決!

+0

ありがとう!出来た :) –

関連する問題