make_voteable icon indicating copy to clipboard operation
make_voteable copied to clipboard

Total votes cached count

Open hulihanapplications opened this issue 14 years ago • 4 comments
trafficstars

Should make_voteable models have a cached count for the voteable record, in addition to up_votes and down_votes? Right now, it seems there's no easy way to order records by total votes, only up_votes and down_votes.

For example, you'd add this to your database columns:

add_column :comments, :votes, :integer, :default => 0    

up_votes - down_votes would then be stored in in this attribute whenever up_vote or down_vote are called.

hulihanapplications avatar Jul 05 '11 19:07 hulihanapplications

It should be pretty easy to do that with a SQL where clause (http://stackoverflow.com/questions/3059138/mysql-order-by-sum-of-columns) and I guess it is pretty easy by using the ActiveRecord query syntax, too. Have you tried something like that? Not sure about performance.

medihack avatar Jul 06 '11 15:07 medihack

If this won't help I could indeed add such an additional field ... no prob.

medihack avatar Jul 06 '11 15:07 medihack

Yeah that works. A small performance to do the addition, but nothing bad. An additional field might be beneficial for users not interested in adding the extra scope. Here's what my little scope looks like:

scope :most_votes_first, order("up_votes - down_votes DESC")

hulihanapplications avatar Jul 06 '11 16:07 hulihanapplications

Would it be possible to add the cached votes column? I think it would make the code cleaner and more performant. Thank you

andreierdoss avatar Apr 14 '12 21:04 andreierdoss