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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:28:53+00:00 2026-05-29T11:28:53+00:00

I have a Post, Comment and Vote model Each time an instance of Vote

  • 0

I have a Post, Comment and Vote model

Each time an instance of Vote model is created (either with :polarity +1 or -1) it updates the total_votes column of the post it belongs to:

vote.rb:

class Vote < ActiveRecord::Base
  belongs_to :votable, :polymorphic => true
  belongs_to :user

  before_create :update_total

  protected

  def update_total
    total_average = self.votable.total_votes
    self.votable.update_attribute(:total_votes, total_average + self.polarity)
  end
end

This is how I call it in the show.html.erb view:

  <div class="post-<%= @post.id %>">
   <h3><span class="vote-count"><%= @post.total_votes %></span> votes</h3><br />

example of votes_controller.rb:

 def vote_up
    @post = Post.find(params[:id])

    if @post.votes.exists?(:user_id => current_user.id)
      @notice = 'You already voted'
    else
      @vote = @post.votes.create(:user_id => current_user.id, :polarity => 1)
    end

    respond_to do |format|
      format.js
    end
  end

routes.rb:

 get 'votes/:id/vote_up' => 'votes#vote_up', as: 'vote_up'

For some reason @post.total_votes gives 0, the default of the column (whether its actual value is -1 or 1) if it is attached to show.html.erb via Ajax from this file:

vote_up.js.erb:

<% unless @notice.blank? %>
  alert("<%= @notice %>");
<% end %>

<% unless @vote.blank? %>
  $('.post-<%=@post.id%> span.vote-count').html('<%= @post.total_votes %>');
  $('.post-<%=@post.id%> div.voted-user').html('<% @post.votes.each do |vote| %><%= link_to vote.user.username, vote.user %><% end %>');
<% end %>

Any suggestion to fix this?

  • 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-29T11:28:53+00:00Added an answer on May 29, 2026 at 11:28 am

    You’re loading the @post object, then changing it in your Vote model’s callbacks, but the @post object doesn’t know about this change and continues to use its cached results from the database. You can force it to reload by putting a @post.reload right after your @post.votes.create line.

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

Sidebar

Related Questions

I have Post and Comment model. I want to achieve the following: Each time
I have two models related one-to-many: a Post and a Comment : class Post(models.Model):
I have Comment as a polymorphic model. It is attached to Post, Review, etc.
I have a Post model that has a polymorphic association with a Vote model:
I have a Post model and a Comment model (which is a nested resource
I have 3 model, User, Post, Comment with definition like below class Post <
I have a Post Class and a Comment Class. I have a post object
I have a Comment class with a :foreign_key of post_id in the Post class.
I have a model called Vote and this is what vote.rb looks like: class
Right now, I have three models Post, Comment and User (using Devise ) associated

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.