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?
You’re loading the
@postobject, then changing it in yourVotemodel’s callbacks, but the@postobject 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.reloadright after your@post.votes.createline.