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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:13:41+00:00 2026-05-25T10:13:41+00:00

Okay, lets say I got 2 different Models: Poll (has_many :votes) Vote (belongs_to :poll)

  • 0

Okay, lets say I got 2 different Models:

  1. Poll (has_many :votes)
  2. Vote (belongs_to :poll)

So, one poll can have many votes.
At the moment I’m displaying a list of all polls including its overall vote_count.


Everytime someone votes for a poll, I’m going to update the overall vote_count of the specific poll by using:

@poll = Poll.update(params[:poll_id], :vote_count => vote_count+1)

To retrieve the vote_count I use : @poll.vote_count which works fine.

Lets assume I got a huge amount of polls (in my db) and a lot of people would vote for the same poll at the same time.


Question :
Wouldn’t it be more efficient to remove the vote_count from the poll table and use:
@vote_count = Poll.find(params[:poll_id]).votes.count
for retrieving the overall vote_count instead?
Which operation (update vs. count) would make more sense in this case?
(I’m using postgresql in production)

Thanks for the help!

  • 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-25T10:13:42+00:00Added an answer on May 25, 2026 at 10:13 am

    Have you considered using a counter cache (see counter_cache option)? Rails has this built in functionality to handle all the possible updates to an association and how that would affect the counter.

    It’s as simple as adding a 0 initialized integer column named #{attribute.pluralize}_count(in your case votes_count) on the table of one to many side of an association (in your case Poll).

    And then on the other side of the association add the :counter_cache => true argument to the belongs to statement.

    belongs_to :poll, :counter_cache => true
    

    Now this doesn’t answer your question exactly, and the correct answer will depend on the shape of your data and the indexes you’ve configured. If you’re expecting your votes table to number in the millions spread out over thousands of polls, then go with the counter cache, otherwise just counting the associations should be fine.

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

Sidebar

Related Questions

How can I re-use a function? Okay lets say I have this main function
Okay, this one is pretty complicated... lets say I have an array called $data
Okay lets say I have a list, and I want to check if that
okay, so I have an int lets say called Var1: public const int Var1
Okay lets say I have a basic demographic content type (Client) that I am
Okay, lets say I have a URL like so, which is mapped via HTTP
Okay, so basically lets say i have a matrix: matrix([[0, 1, 2, 3, 4],
Okay, lets say I have the following text... <h2><a name=test1></a>Test 1</h2>test 1 text <h2><a
okay so lets say $word = abcdefg@hijk.lmn can i make a new variable only
Okay so lets say I have a list of addresses in a text file

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.