Skip to content

Commit

Permalink
Merge pull request rubysherpas#21 from jamescook/issue_20_fix_pgsql_s…
Browse files Browse the repository at this point in the history
…cope_bug

Fix Topic.by_most_recent_post to be compatible with Postgres
  • Loading branch information
radar committed Jun 29, 2011
2 parents a88b7f6 + 394019f commit fc4e065
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/models/forem/topic.rb
Expand Up @@ -15,7 +15,7 @@ class Topic < ActiveRecord::Base

scope :visible, where(:hidden => false)
scope :by_pinned, order('forem_topics.pinned DESC, forem_topics.id')
scope :by_most_recent_post, joins(:posts).order('forem_posts.created_at DESC, forem_topics.id').group('topic_id')
scope :by_most_recent_post, joins(:posts).select("DISTINCT forem_posts.topic_id, forem_topics.*").order('forem_posts.created_at DESC, forem_topics.id')
scope :by_pinned_or_most_recent_post, includes(:posts).
order('forem_topics.pinned DESC').
order('forem_posts.created_at DESC').
Expand Down
16 changes: 16 additions & 0 deletions spec/models/topic_spec.rb
Expand Up @@ -45,4 +45,20 @@
Forem::Topic.by_pinned.first.should == @topic2
end
end

describe ".by_most_recent_post" do
before do
Forem::Topic.delete_all
@topic1 = Factory(:topic)
Factory(:post, :topic => @topic, :created_at => 1.seconds.ago)
@topic2 = Factory(:topic)
Factory(:post, :topic => @topic2, :created_at => 5.seconds.ago)
@topic3 = Factory(:topic)
Factory(:post, :topic => @topic3, :created_at => 10.seconds.ago)
end

it "should show topics by most recent post" do
Forem::Topic.by_most_recent_post.to_a.should == [@topic1, @topic2, @topic3]
end
end
end

0 comments on commit fc4e065

Please sign in to comment.