Search code examples
ruby-on-railsacts-as-votable

Rails: count number of total votes from a list of posts (acts_as_votable)


I have a list of posts and all of them can be votable. I can count the number of votes for each post, but how can I count the number for all of them? I'm using the gem acts_as_votable for the voting system

I count the number of posts like this: <%= performance_indicator.improvement_actions.count %>

this is my "posts" controller:

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

And I want to put here the counter:

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

Solution

  • Do you have any cache column for votes in the ImprovementAction model? (https://github.com/ryanto/acts_as_votable#caching)

    It is for keeping total amount of votes for each post. You should have it to do the calculation you want:

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

    This will make only one database request.

    Never do like this:

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

    This will have to load and instantiate all the records and make a separate request for each of them to retrieve their votes. Very BAD solution !