Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6249323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:12:57+00:00 2026-05-24T13:12:57+00:00

The model Article has an attribute :rating. This attribute is updated everytime someone rates

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T13:12:58+00:00Added an answer on May 24, 2026 at 1:12 pm

    Either of those two will do the job, depending on which style you prefer:

    @article = Article.find(:first, :order => 'rating DESC')
    
    @article = Article.order('rating DESC').first
    

    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:

    @top_article = @articles.sort { |a1,a2| a2[:rating] <=> a1[:rating] }.first
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have hibernate database with three model: Article - which has is part of
I have model Article it has field title with some text that may contain
after read this article i tried generate EF model by System.Diagnostics.Process: Process myProcess =
This may be old news but back in March 2009, this article, Model-View-ViewModel In
I have a model that has an attribute that is an Array. What's the
Have an article model, each article has an author and publisher (both are tables).
I have a weird design question. I have a model called Article, which has
I have an Article model that has a foreign key relationship with Author that
I have a pretty generic Article model, with m2m relation to Tag model. I
From the MSDN article on STAThread: Indicates that the COM threading model for an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.