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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:08:55+00:00 2026-06-12T02:08:55+00:00

I am trying to create a voting system. The models that are being voted

  • 0

I am trying to create a voting system. The models that are being voted on are Issues and I have another model called Vote that holds an issue_id and a vote value of 0 or 1. The votes are being created with forms with hidden fields. This is on the issues index view.

<h1>Votes</h1>

<% @issues.each do |issue| %>
<li>
    <div class="issue">
        <h2><%= issue.title %></h2>
        <p><%= issue.body %></p>

        <%= form_for(@vote, :remote => true) do |f| %>
        <%= f.hidden_field "issue_id", :value => issue.id %>
        <%= f.hidden_field "vote", :value => 1 %>
        <%= submit_tag "Up", :class => 'up-vote' %>
        <% end %>


        <%= form_for(@vote, :remote => true) do |f| %>
        <%= f.hidden_field "issue_id", :value => issue.id %>
        <%= f.hidden_field "vote", :value => 0 %>
       <%= submit_tag "Down", :class => 'down-vote' %>
        <% end %>

    </div>
</li>
<% end %>

There is a has_many and belongs_to relationship between issues and votes. I want to display the number of up votes and down votes next to the buttons for each issue. So I need to pull all the votes with vote = 1 for each issue, as well as all the ones with vote = 0, and count each. Also want to know total votes. How should I do this? I have counter_cache set on the vote models issue association and votes_count column in my issues schema. Should this work be done in the controller or the model?

  • 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-06-12T02:08:56+00:00Added an answer on June 12, 2026 at 2:08 am

    I haven’t tried it myself, but you should try adding some methods to your Issue model like this:

    def upvote_count
      votes.count(:conditions => "value = 1")
    end
    
    def downvote_count
      votes.count(:conditions => "value = 0")
    end
    

    I learned this in the Rails documentation. You can see for yourself here:

    http://ar.rubyonrails.org/classes/ActiveRecord/Calculations/ClassMethods.html
    http://apidock.com/rails/ActiveRecord/Associations/CollectionAssociation/count

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

Sidebar

Related Questions

I have am trying to create a simple voting page. There are a few
I'm trying to create a Stackoverflow like voting system, and I've run into a
I'm trying to display all the users I have not voted on yet! CREATE
Hi I am trying create a system call that will count the number of
I was having trouble with a voting system that I'm trying to make in
I have a voting system and I am trying to write a query for
I want to create a voting system, where multiple domain objects can be voted
I am trying create a trending system that displays which keywords are trending by
I'm trying to create a function that disables voting button after an Ajax POST
hi I'm trying create chat using node.js I see example in http://chat.nodejs.org/ I have

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.