0

我有一个帖子列表,所有帖子都可以投票。我可以计算每个帖子的票数,但我如何计算所有帖子的票数?我正在使用 gemacts_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 %>
4

1 回答 1

1

在 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) 

这将只发出一个数据库请求。

永远不要这样做:

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

这将必须加载和实例化所有记录,并为每个记录发出单独的请求以检索他们的投票。非常糟糕的解决方案!

于 2016-03-22T22:08:43.737 回答