make_voteable
make_voteable copied to clipboard
Total votes cached count
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.
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.
If this won't help I could indeed add such an additional field ... no prob.
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")
Would it be possible to add the cached votes column? I think it would make the code cleaner and more performant. Thank you