Skip to content
This repository has been archived by the owner on Nov 13, 2021. It is now read-only.

Average rating and total number of ratings together #72

Open
ghost opened this issue Jun 20, 2014 · 1 comment
Open

Average rating and total number of ratings together #72

ghost opened this issue Jun 20, 2014 · 1 comment

Comments

@ghost
Copy link

ghost commented Jun 20, 2014

Hi,

I am using activerecord_reputation_system gem to rate something, let's call it model X. In app/models/x.rb I have

has_reputation :rating, source: :user, aggregated_by: :average

This lets me view the average rating.

My question: how can I view both average rating and total number of votes for model X? One way would be to add to app/models/x.rb:

has_reputation :rating, source: :user, aggregated_by: :average
has_reputation :reviews, source: :user, aggregated_by: :sum

and in the controller have:

value = params[:rating]
@x = X.find(params[:id])
if X.where(id: params[:id]).evaluated_by(:reviews, current_user).blank?
  @x.add_or_update_evluation(:reviews, 1, current_user)
end
@x.add_or_update_evaluation(:rating, value, current_user)

Is this the best way to do it or is there a better way?

Thanks a lot.

@toobulkeh
Copy link

It may not be built into the system, but you can probably do a custom query against ReputationSystem::Evaluation.all.count replace all with your own filter. Your controller is essentially storing an extra evaluation for each one, which is duplicating data.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant