My api has Posts. Each post has a number of upvotes and downvotes. I wish to update the percentage of upvotes and downvotes each time an upvote/downvote is made.
In my VotesController I have:
#PUT /downvote/:id
def downvote
@post = Post.find(params[:id])
@post.downvotes = @post.downvotes + 1
@post.recalculate_percentages()
@post.save
render json:@post
end
In my Post model I have a method that refreshes these values:
def recalculate_percentages
self.downvotes_percentage = self.downvotes / (self.downvotes + self.upvotes)
self.upvotes_percentage = self.upvotes / (self.downvotes + self.upvotes)
end
Why are these changes not having any effect. In the rendered JSON the values for the percentages remain at 0.