Ok, So I have a review model, and a votes model (Upvotes and Downvotes modeled through a single variable 'likes', If its true its an upvote, if its false its a downvote.). The votes are a has_many relation, and are polymorphic (I hope to be able to use this on more than just reviews later on).
So in the reviews model has the line
has_many :votes, :as => :votable
And the votes model is defined like this
class Vote < ActiveRecord::Base
belongs_to :votable, :polymorphic => true
belongs_to :user
def upvote?
like
end
def downvote?
!like
end
end
Now this is fine, I can upvote, downvote and remove votes just fine. But I'm trying to generate a score bassed on the upvotes and downvotes (Simply upvotes-downvotes for now). To do this I added the following to the reviews model
def score
up = 0
down = 0
self.votes.each do |v|
if v.upvote?
up += 1
elsif v.downvote?
down += 1
end
end
up-down
end
However I always get 0 back as the answer. The error is in the loop, as I can set the up or down variables to be what ever outside of it and they are passed through. Its starting to drive me insane, and I have no idea where im going wrong.