0
我需要使用百分比的votes_count。通過使用counter_cache軌道進行百分比計數4
我的關係是
celebrity.rb
has_many :votes
vote.rb
belongs_to :celebrity, counter_cache: true
我控制器
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
我怎樣才能使百分比的votes_count?票數/總數票* 100