Merge pull request #54 from cblanc/recent_topic_fix

Rewrite recent_topic sql to return unique topics on subquery
This commit is contained in:
simplefl 2015-09-05 22:56:15 +02:00
commit 14d044bd26
2 changed files with 5 additions and 4 deletions

View file

@ -44,10 +44,11 @@ class Topic < ActiveRecord::Base
def self.recent_topics def self.recent_topics
find_by_sql %q{ find_by_sql %q{
SELECT DISTINCT topics.* SELECT DISTINCT topics.*
FROM (SELECT id, topic_id FROM (SELECT max(id) as max_id, topic_id
FROM posts FROM posts
ORDER BY id DESC GROUP BY topic_id
LIMIT 20) AS T ORDER BY max_id DESC
LIMIT 200) AS T
INNER JOIN topics INNER JOIN topics
ON T.topic_id = topics.id ON T.topic_id = topics.id
INNER JOIN forums INNER JOIN forums

View file

@ -31,7 +31,7 @@ describe Topic do
describe ".recent_topics" do describe ".recent_topics" do
it "returns 5 unique, most recently posted topics" do it "returns 5 unique, most recently posted topics" do
topics = [] topics = []
6.times do 10.times do
topic = create :topic topic = create :topic
topics << topic topics << topic
3.times { create :post, topic: topic } 3.times { create :post, topic: topic }