Skip to content

Commit

Permalink
performance improvement by eager loading stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
marten committed Aug 6, 2008
1 parent 5a7fa7a commit 662c415
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 11 deletions.
4 changes: 2 additions & 2 deletions app/controllers/topics_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ class TopicsController < ApplicationController
# GET /topics
# GET /topics.xml
def index
# TODO Here be unoptimized code.
@topics = Topic.find(:all).sort {|a,b| b.posts.last.created_at <=> a.posts.last.created_at }
@topics = Topic.find(:all, :include => [:tags, :posts])
@topics.sort! {|a,b| b.posts.last.created_at <=> a.posts.last.created_at }
@tags = Topic.tag_counts.sort {|a,b| b.count <=> a.count }[0..19]

respond_to do |format|
Expand Down
9 changes: 2 additions & 7 deletions app/models/topic.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
class Topic < ActiveRecord::Base
acts_as_taggable

has_many :posts
has_many :posts, :include => :user, :dependent => :destroy
has_many :posters, :through => :posts, :source => :user

has_one :starter, :class_name => 'User', :through => :posts, :source => :user,
:order => '"posts".created_at ASC'
has_one :latestr, :class_name => "User", :through => :posts, :source => :user,
:order => '"posts".created_at DESC'

has_many :viewings
has_many :viewers, :through => :views, :source => :user

Expand All @@ -24,7 +19,7 @@ def read_by(user)
v.seen = Time.now
v.save
end

private
def sanitize_fields
self.title = helpers.sanitize(title)
Expand Down
4 changes: 2 additions & 2 deletions app/views/topics/_list.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
%li.tags
= "Tags: " + tags_for_topic(topic.tags)
/%li.start
/ = "Started by: " + link_to(topic.starter.name, topic.starter)
/ = "Started by: " + link_to(topic.posts.first.name, topic.posts.first)
%li.last
= "Last post by: " + link_to(topic.latestr.name, topic.latestr)
= "Last post by: " + link_to(topic.posts.last.user.name, topic.posts.last.user)
= " at " + link_to(topic.posts.last.created_at.to_formatted_s(:short), topic_post_path(topic, topic.posts.last))

0 comments on commit 662c415

Please sign in to comment.