Rails:计算帖子列表中的总投票数(acts_as_votable)

时间:2016-03-22 20:23:13

标签: ruby-on-rails acts-as-votable

我有一个帖子列表,所有帖子都可以投票。我可以计算每个帖子的投票数,但我如何计算所有帖子的数量?我使用gem acts_as_votable作为投票系统

我会计算这样的帖子数量:<%= performance_indicator.improvement_actions.count %>

这是我的&#34;帖子&#34;控制器:

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 %>

1 个答案:

答案 0 :(得分: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) 

这只会产生一个数据库请求。

永远不要这样:

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

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