注意:我在这个例子中关注http://railscasts.com/episodes/364-active-record-reputation-system。
我有一个 Location 模型,我让用户对项目进行排名(即 1 到 5 星):
has_reputation :location_rating, 来源: :user, aggregated_by: :average
在我的控制器中,我要么删除评级:
@location.delete_evaluation(:location_rating, current_user)
或更新它:
@location.add_or_update_evaluation(:location_rating, params[:rating] , current_user)
我对如何查询感到非常困惑:
- 所有用户对某个位置的当前平均评分
- 给定用户对给定位置的评分
- 给定位置的评分计数
我正在尝试@location.reputation_for(:location_rating)
并得到一些奇怪的行为。如果给定的用户删除了一个评分,那么新的reputation_for 值似乎在旧评分中取平均值。
例如:
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 |
+----+-----------------+-----------+-------------+-----------+-------------+-------+-------------------------+-------------------------+
在 Location id = 5 上,我的声誉不应该是 (3 + 5) / 2 = 4 吗?我目前得到 2.59375。我将用户 11 的评分更新为 1,新的声誉为 0.59375。
我确信我在这里遗漏了一些明显的东西(因为我是 Rails 新手)。