Category featured users: show most recent posters instead of most frequent posters

This commit is contained in:
Neil Lalonde 2013-12-18 10:51:26 -05:00
parent 14d21ec865
commit 8057373584
1 changed files with 15 additions and 11 deletions

View File

@ -7,21 +7,25 @@ class CategoryFeaturedUser < ActiveRecord::Base
end end
def self.feature_users_in(category) def self.feature_users_in(category)
# Figure out major posters in the category # Figure out most recent posters in the category
user_counts = exec_sql " most_recent_user_ids = exec_sql "
SELECT p.user_id, SELECT x.user_id
COUNT(*) AS category_posts FROM (
FROM posts AS p SELECT DISTINCT ON (p.user_id) p.user_id AS user_id,
INNER JOIN topics AS ft ON ft.id = p.topic_id p.created_at AS created_at
WHERE ft.category_id = :category_id FROM posts AS p
GROUP BY p.user_id INNER JOIN topics AS ft ON ft.id = p.topic_id
ORDER BY category_posts DESC WHERE ft.category_id = :category_id
LIMIT :max_featured_users AND p.user_id IS NOT NULL
ORDER BY p.user_id, p.created_at DESC
) AS x
ORDER BY x.created_at DESC
LIMIT :max_featured_users;
", category_id: category.id, max_featured_users: max_featured_users ", category_id: category.id, max_featured_users: max_featured_users
transaction do transaction do
CategoryFeaturedUser.delete_all category_id: category.id CategoryFeaturedUser.delete_all category_id: category.id
user_counts.each do |uc| most_recent_user_ids.each do |uc|
create(category_id: category.id, user_id: uc['user_id']) create(category_id: category.id, user_id: uc['user_id'])
end end
end end