Note that there are some explanatory texts on larger screens.

plurals
  1. PORoR voting system. Counting votes by condition vote = true or vote = false not working
    primarykey
    data
    text
    <p>I have these models</p> <ul> <li>Issue</li> <li>Vote</li> </ul> <p>Issue has_many votes and Vote belongs_to issue. The Vote model has a boolean vote attribute. On the issues index view I want to cycle through the issues and display the title, body, an up vote button, a down vote button, and respective labels that show how many up votes and down votes there currently is. I do this with a form with hidden fields for issue_id and vote (1 or 0). Methods on the Issue model are supposed to count the votes. But I keep getting 0 returned. Totalvotes_count works but the other two don't. In the server log I see the votes being created with the proper issue_id and vote value but the queries aren't working for some reason. </p> <p><strong>Issue model</strong></p> <pre><code>class Issue &lt; ActiveRecord::Base attr_accessible :body, :end_at, :title validates_presence_of :title, :body, :end_at has_many :votes def upvotes_count votes.count(:conditions =&gt; "vote = 1") end def downvotes_count votes.count(:conditions =&gt; "vote = 0") end def totalvotes_count votes.count end end </code></pre> <p><strong>index.html.erb</strong></p> <pre><code>&lt;% @issues.each do |issue| %&gt; &lt;li&gt; &lt;div class="issue"&gt; &lt;h2&gt;&lt;%= issue.title %&gt;&lt;/h2&gt; &lt;p&gt;&lt;%= issue.body %&gt;&lt;/p&gt; &lt;%= form_for(@vote, :remote =&gt; true) do |f| %&gt; &lt;%= f.hidden_field "issue_id", :value =&gt; issue.id %&gt; &lt;%= f.hidden_field "vote", :value =&gt; 1 %&gt; &lt;%= submit_tag issue.upvotes_count.to_s + " Up", :class =&gt; 'up-vote' %&gt; &lt;% end %&gt; &lt;%= form_for(@vote, :remote =&gt; true) do |f| %&gt; &lt;%= f.hidden_field "issue_id", :value =&gt; issue.id %&gt; &lt;%= f.hidden_field "vote", :value =&gt; 0 %&gt; &lt;%= submit_tag issue.downvotes_count.to_s + " Down", :class =&gt; 'down-vote' %&gt; &lt;% end %&gt; &lt;/div&gt; &lt;/li&gt; &lt;% end %&gt; </code></pre> <p><strong>Votes Controller</strong></p> <pre><code>class VotesController &lt; ApplicationController def index @votes = Vote.find(:all, :include =&gt; :issue) end def new @vote = Vote.new(params[:vote]) respond_to do |format| format.html # new.html.erb format.xml { render :xml =&gt; @vote } end end def create @vote = Vote.new(params[:vote]) respond_to do |format| if @vote.save format.js format.html { redirect_to issues_path } else format.html { redirect_to issues_path } end end end end </code></pre> <p><strong>Issues controller (partial)</strong></p> <pre><code>class IssuesController &lt; ApplicationController # GET /issues # GET /issues.json def index @issues = Issue.all @vote = Vote.new respond_to do |format| format.html # index.html.erb format.json { render json: @issues } end </code></pre> <p>end</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload