From 586c9a7258b92adb6d493f56996c7e42bcb80064 Mon Sep 17 00:00:00 2001 From: albert Date: Sat, 23 Feb 2013 19:46:32 -0500 Subject: [PATCH] fix favcount --- app/logical/post_query_builder.rb | 6 ++++++ app/models/post.rb | 2 ++ script/fixes/003.sql | 2 ++ 3 files changed, 10 insertions(+) create mode 100644 script/fixes/003.sql diff --git a/app/logical/post_query_builder.rb b/app/logical/post_query_builder.rb index b88669513..ec2a85961 100644 --- a/app/logical/post_query_builder.rb +++ b/app/logical/post_query_builder.rb @@ -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 diff --git a/app/models/post.rb b/app/models/post.rb index 42b80e7aa..2450d03e6 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -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 diff --git a/script/fixes/003.sql b/script/fixes/003.sql new file mode 100644 index 000000000..aa5f8ee9f --- /dev/null +++ b/script/fixes/003.sql @@ -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';