fix favcount

This commit is contained in:
albert
2013-02-23 19:46:32 -05:00
parent 3b4222cacd
commit 586c9a7258
3 changed files with 10 additions and 0 deletions

View File

@@ -207,6 +207,12 @@ class PostQueryBuilder
when "score_asc"
relation = relation.order("posts.score, posts.id DESC")
when "favcount"
relation = relation.order("posts.fav_count DESC, posts.id DESC")
when "favcount_asc"
relation = relation.order("posts.fav_count, posts.id DESC")
when "mpixels", "mpixels_desc"
# Use "w*h/1000000", even though "w*h" would give the same result, so this can use

View File

@@ -490,6 +490,7 @@ class Post < ActiveRecord::Base
def add_favorite!(user)
return if favorited_by?(user.id)
append_user_to_fav_string(user.id)
increment!(:fav_count)
user.add_favorite!(self)
end
@@ -499,6 +500,7 @@ class Post < ActiveRecord::Base
def remove_favorite!(user)
return unless favorited_by?(user.id)
decrement!(:fav_count)
delete_user_from_fav_string(user.id)
user.remove_favorite!(self)
end

2
script/fixes/003.sql Normal file
View File

@@ -0,0 +1,2 @@
set statement_timeout = 0;
update posts set fav_count = (select count(*) from favorites _ where _.post_id = posts.id) where posts.created_at > '2013-02-01';