jeudi 3 septembre 2015

make count in percentage by using counter_cache rails 4

I need to make votes_count in percentage.

My relations are

celebrity.rb

has_many :votes

vote.rb

belongs_to :celebrity, counter_cache: true

My controller

def show_celebrity
  @celebrity = Celebrity.includes(:category).where('category_id = ?', params[:id])
  @celebrity.each do |celeb|
    celeb["votes_count"] = celeb.votes.count
  end
  respond_to do |format|
    format.json { render json: @celebrity }
  end
end

how can I make the votes_count in percentage? votes / total no. of votes *100

Aucun commentaire:

Enregistrer un commentaire