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 1034783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:26:47+00:00 2026-05-16T14:26:47+00:00

I have a model that has ratings in it for an post. I want

  • 0

I have a model that has ratings in it for an post.

I want to display a list of the top 5 ratings for a given post, but I am completely lost on where to start. I figure the find method might have something useful, but I’m unsure. I’ve even considered looping through each record getting its size, adding it to a hash, sorting the hash, etc., but that seems too complicated.

Does anyone know how I might accomplish something like this?

Thank you

Edit: I found this to get all the posts that have the rating of agree:

Post.find(:all, :include => :post_ratings, :condtions => ['post_ratings.agree = ?', true])

The only problem is I can’t figure out how to get the top five ratings from this query.

  • 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-16T14:26:47+00:00Added an answer on May 16, 2026 at 2:26 pm

    Might be worth giving a little more of an example of the code you’re working with but I’ll answer with a few assumptions.

    If you have:

    class Post
      has_many :post_ratings
    end
    
    class PostRating
      belongs_to :post
    
      # Has a 'rating' attribute
    end
    

    You can find the top five post ratings with:

    p = Post.find(:first) # For example
    p.post_ratings.find(:all, :limit => 5, :order => 'rating desc')
    

    To get the top five post ratings overall you can do:

    PostRating.find(:all, :limit => 5, :order => 'rating desc')
    

    UPDATE:

    Following your edit it seems you have an ‘agree’ and a ‘disagree’ column. Not sure how that works in combination so I’ll stick with the ‘agree’ column. What you’ll need to do is count the ratings with agree flagged. Something like:

    count_hsh PostRating.count(:group => 'post_id', 
                               :order => 'count(*) desc', 
                               :conditions => { :agree => true },
                               :limit => 5)
    

    This will return you a hash mapping the post id to the count of agree ratings. You can then use that post_id to locate the posts themselves. The ratings are provided by the counts so the individual ratings are (I think) of no use though you could access them by calling post.post_ratings.

    So, to get the top five posts:

    @top_five_posts = []
    count_hsh.each_pair do |post_id, ratings|
      p = Post.find(post_id)
      p[:rating_count] = ratings
      @top_five_posts << p
    end
    

    This is probably more verbose than it could be but is hopefully illustrative. The p[:rating_count] is a virtual attribute which isn’t in the database but will allow you to access the .rating_count method on the posts in your view if you wish to.

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

Sidebar

Related Questions

I have a model that has a bunch of attributes but the two of
I have a model that has a CharField and in the admin I want
I have a model that has an image attached to it that I want
I have a model that has a boolean called draft I want to validate
I have a model that has fields of name:string , default:boolean . I want
I have a model that has a ForeignKey to the built-in user model in
I have a model that has an arbitrary number of children entities. For simplicity
I have category model that has a tree structure. In my database I have
I have a User model that has many posts. Im trying to render the
I have a Workout model that has and belongs to many Equipment models. I

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.