# Topic Ratings Plugin

**URL:** <https://meta.discourse.org/t/topic-ratings-plugin/39578>\
**Category:** Plugin\
**Tags:** pavilion\
**Created:** [February 14, 2016, 9:06pm UTC](https://meta.discourse.org/t/topic-ratings-plugin/39578 "2016-02-14T21:06:53Z")\
**Posts on this page:** 1\
**Showing post:** 27

<div class="post-metadata">

**Author:** ![angus](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/angus/32/341715_2.png) [@angus](https://meta.discourse.org/u/angus)\
**Post date:** [March 7, 2016, 1:20pm UTC](https://meta.discourse.org/t/topic-ratings-plugin/39578/27 "2016-03-07T13:20:13Z")

</div>

> [@fackelwind](#):
>
> I have currently the votes 0,4,2 which result in a one star rating. But I swear that before the update, changing that 2 into a 3 was enough to give me an average 2 star rating.I have now changed the ratings to 0,4,5 and raised the rating of the deleted comment to 5. I still only get a 1 star average.Is the rating maybe not getting updated at all anymore? There are two more comments in the thread without a rating (no stars shown).

Indeed. I had to add a ‘rating\_weight’ property to accommodate deleted posts with ratings (and perhaps to use in the future for weighting ratings differently according to poster, or other metadata such as likes). This subsequently changed the calculation of the topic average from this:

```plaintext
@topic_posts = Post.where(topic_id: post.topic_id)
@all_ratings = PostCustomField.where(post_id: @topic_posts.map(&:id), name: "rating").pluck('value').map(&:to_i)
average = @all_ratings.inject(:+).to_f / @all_ratings.length

```

to this:

```plaintext
@topic_posts = Post.with_deleted.where(topic_id: post.topic_id)
@ratings = []
@topic_posts.each do |post|
  weight = post.custom_fields["rating_weight"]
  if weight.blank? || weight.to_i > 0
    rating = post.custom_fields["rating"].to_i
    @ratings.push(rating)
  end
end
average = @ratings.inject(:+).to_f / @ratings.length

```

The `rating_weight` of deleted posts is set to 0. This logic (I think) accommodates rating weight by only counting ratings if the weight is greater than 0 or is blank (to support posts with ratings posted prior to this change). However, the problem is that it also counts posts without a rating at all.. So I’ve changed it to:

```plaintext
@topic_posts = Post.with_deleted.where(topic_id: post.topic_id)
@ratings = []
@topic_posts.each do |post|
  weight = post.custom_fields["rating_weight"]
  if post.custom_fields["rating"] && (weight.blank? || weight.to_i > 0)
   rating = post.custom_fields["rating"].to_i
   @ratings.push(rating)
  end
end
average = @ratings.inject(:+).to_f / @ratings.length 

```

This also checks if the post has a rating before counting it. A silly mistake on my part really. The problem is that I’m always working on this either early in the morning before work or late at night when the brain is softer…

I hope that fixes it.

[https://github.com/angusmcleod/discourse-ratings/commit/615e2388e7475077e348241113fe335a631188b0](https://github.com/angusmcleod/discourse-ratings/commit/615e2388e7475077e348241113fe335a631188b0)

---

_[View the full topic](https://meta.discourse.org/t/topic-ratings-plugin/39578)._
