Note: I am following http://railscasts.com/episodes/364-active-record-reputation-system for this example.
I have a Location model where I am letting the user rank (i.e. 1 to 5 stars) the item:
has_reputation :location_rating, source: :user, aggregated_by: :average
In my controller I am either deleting the rating:
@location.delete_evaluation(:location_rating, current_user)
or updating it:
@location.add_or_update_evaluation(:location_rating, params[:rating] , current_user)
I am brutally confused how to query:
- the current average rating of a Location by all users
- the rating of a given Location by a given user
- the count of ratings on a given Location
I am trying @location.reputation_for(:location_rating)
and getting some weird behavior. If a given user deletes a rating and then the new reputation_for value seems to averaging in the old ratings.
For example:
1.9.3p0 :038 > RSEvaluation.all
RSEvaluation Load (1.7ms) SELECT "rs_evaluations".* FROM "rs_evaluations"
+----+-----------------+-----------+-------------+-----------+-------------+-------+-------------------------+-------------------------+
| id | reputation_name | source_id | source_type | target_id | target_type | value | created_at | updated_at |
+----+-----------------+-----------+-------------+-----------+-------------+-------+-------------------------+-------------------------+
| 46 | tracking | 1 | User | 6 | Location | 0.0 | 2012-08-28 05:15:13 UTC | 2012-08-28 05:15:13 UTC |
| 48 | tracking | 1 | User | 5 | Location | 0.0 | 2012-09-04 14:44:37 UTC | 2012-09-04 14:44:37 UTC |
| 51 | tracking | 1 | User | 8 | Location | 0.0 | 2012-09-12 06:51:58 UTC | 2012-09-12 06:51:58 UTC |
| 52 | tracking | 1 | User | 1 | Location | 0.0 | 2012-09-12 14:54:39 UTC | 2012-09-12 14:54:39 UTC |
| 54 | location_rating | 1 | User | 5 | Location | 3.0 | 2012-09-19 05:10:46 UTC | 2012-09-19 05:18:16 UTC |
| 56 | tracking | 11 | User | 5 | Location | 0.0 | 2012-09-19 05:47:10 UTC | 2012-09-19 05:47:10 UTC |
| 58 | location_rating | 11 | User | 3 | Location | 2.0 | 2012-09-19 06:33:12 UTC | 2012-09-19 06:33:12 UTC |
| 61 | location_rating | 11 | User | 5 | Location | 5.0 | 2012-09-19 07:15:42 UTC | 2012-09-19 07:15:42 UTC |
+----+-----------------+-----------+-------------+-----------+-------------+-------+-------------------------+-------------------------+
On Location id = 5 should my reputation not be (3 + 5) / 2 = 4? I currently get 2.59375. I update User 11's rating to 1 and the new reputation is 0.59375.
I am sure I am missing something obvious here (as I am a Rails newbie).