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

  • Home
  • SEARCH
  • 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 662661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:24:11+00:00 2026-05-13T23:24:11+00:00

Let’s say I have some models: User , Post , and Vote . A

  • 0

Let’s say I have some models: User, Post, and Vote. A user has many posts and a post has many votes. Votes can be either an up vote or a down vote (stored as a boolean). What I am considering is the best way to do queries like these:

  1. All users, and the total number of up votes they’ve received.
  2. All users, and the post that has received the most up votes.
  3. All users, and the total number of votes (both up and down) they’ve received.

There are three ways I can think to do this, in increasing levels of efficiency:

  1. Calculate vote counts in the controller using loops. This would potentially do a lot of extra queries and query data that I don’t need, like each post and each vote record. For example (3rd query):

    @users = User.all
    @vote_count = @users.posts.votes.count # retrieves every post and vote, which I don't need
    
  2. Store the vote counts as fields in the User model and use callbacks to update those counts whenever a vote is made. This would make the query simpler, but I’d like to keep the models more loosely coupled and not have the user model’s schema grow every time I need some sort of data on a related model.

  3. Use some sort of query that will do these types of calculations via SQL and not look up more data than I need. This seems like the best choice, but I don’t know how to approach it. Any suggestions/examples? Thanks!

  • 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-13T23:24:11+00:00Added an answer on May 13, 2026 at 11:24 pm

    Use the vote_fu plugin. It supports following methods:

    user.vote_count       # all votes
    user.vote_count(true) # votes for
    user.vote_count(false) # votes against
    posts.votes_count   # all vote count
    posts.votes_for     # votes for
    posts.votes_against # votes against
    posts.votes_total   # votes total
    

    If you don’t want to use the plugin then I would approach your scenario as follows:

    I am assuming the following relationship amongst models.

    class User < ActiveRecord::Base
      has_many :posts
      has_many :votes, :through => :posts
    end
    
    class Post < ActiveRecord::Base
      belongs_to :user
      has_many :votes
    end
    
    class Vote < ActiveRecord::Base
      belongs_to :post
    end
    

    1.1) To count all the votes voted by all the users

    Vote.count  # all
    Vote.count(:conditions => {:vote => true}) # all for
    Vote.count(:conditions => {:vote => false}) # all against
    

    1.2) To find the votes by a user

    user.votes.count # all
    user.votes.count(:conditions => {:vote => true}) # all for
    user.votes.count(:conditions => {:vote => false}) # all against
    

    2.1) Users with most most Up votes

    # You can add the limit clause to restrict the rows
    User.find(:all, :select => "users.*, count(votes.id) AS count", 
                    :joins => [:posts, :votes],  
                    :conditions => [" votes.vote = ? ", true],
                    :group => "votes.id", :order => "count DESC")
    

    2.2) Posts with most Up votes

    # You can add the limit clause to restrict the rows
    Post.find(:all, :select => "posts.*, count(votes.id) AS count", 
                    :joins => [:votes],  
                    :conditions => [" votes.vote = ? ", true],
                    :group => "votes.id", :order => "count DESC")
    

    3.1 ) For a user total number of votes
    Refer to 1.2.1

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

Sidebar

Ask A Question

Stats

  • Questions 511k
  • Answers 511k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Probably most pythonic way: myset = Customer.objects.filter(<something>).order_by(<something>) first, last =… May 16, 2026 at 5:13 pm
  • Editorial Team
    Editorial Team added an answer original (most of this has changed; see updates below) Based… May 16, 2026 at 5:13 pm
  • Editorial Team
    Editorial Team added an answer Its possible but its not cleaner, or faster. Dim t… May 16, 2026 at 5:13 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Let's say I have two entities: Physician Credentials And a physician can have many
Let's say I'm building a data access layer for an application. Typically I have
Let's say you have a class called Customer, which contains the following fields: UserName
Let's say we have a simple function defined in a pseudo language. List<Numbers> SortNumbers(List<Numbers>
Let's say I have a drive such as C:\ , and I want to
Let's say that we have an ARGB color: Color argb = Color.FromARGB(127, 69, 12,
Let's say that I have an arbitrary string like `A man + a plan
Let's say I have a link in a table like: <td class=ms-vb width=100%> <a
Let's say I have a class like this: public class Person { private String
Let's say I have this: public DefaultListModel model = new DefaultListModel(); how do 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.