yes I know how to perform the calculation, and yes I am storing it in my Post model. I do need a Vote model because I'm storing other information as well for the Vote. I did not mention this originally because I was trying to keep my original post simple. What I'm trying to figure out is how to update the Post model with the updated average from a submit of the votes/add controller. I have the Vote model configured with a "belongsTo" relationship to the Post model but I'm not sure how to actually update the Post with the new value.
thanks, dan On Nov 4, 12:05 pm, Robert P <[email protected]> wrote: > Perhaps the most efficient way of doing this is by storing the total > in your Posts model, which might even remove the need of a Votes > model. I'm presuming that when you say Votes have a percentage you > mean that each Votes score is on a range from 0 to 100. > > If you add the columns "average_vote" (float) and "total_votes" (int) > to your Posts table then you can recalculate the average every vote > with the following equation: > new average = ((average vote * total votes) + new vote) / (total votes > + 1) > and of course increment the total_votes column. > > So the pseudocode for Post::addVote(score) is > if post exists > find average_vote and total_votes > set average_vote to ((average_vote * total_votes) + score) / > (total_votes + 1) > set total_votes to total_votes + 1 > > On Nov 4, 8:31 am, djo26 <[email protected]> wrote: > > > Hi everyone, I have Posts that have Votes, and the Vote has a > > percentage. Every time a new Vote is added I want to calculate the > > average percentage of all Votes of that particular Post and update the > > Post average percentage. Besides a custom query I have no idea how to > > do this in my model. Anyone else on here know? I'm relatively new to > > this so it might be simple, but I'm not sure how to do it right now. > > > Any help is appreciated. > > thanks, dan --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~----------~----~----~----~------~----~------~--~---
