The model Article has an attribute :rating. This attribute is updated everytime someone rates the article and the average rating is calculated and stored in the database.
Now I want to get the Article with the highes rating. But how?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Either of those two will do the job, depending on which style you prefer:
Edit:
If you already have an array
@articles, and you have a good reason why you can’t or shouldn’t run a separate SQL query, you can do something like this:Obsolete:
I wonder though, how you can compute the average rating after every new vote, if you only save the average in the database? If the current average rating is 5, and I submit a new rating 10, the number of ratings must be known in order to calculate the new average. How this is done may be relevant for the solution to your problem.