Commit 00b9fc3d8809ac876967b698e4d1e60eb1cb5bd0
1 parent
7e1172c4
Exists in
theme-brasil-digital-from-staging
and in
9 other branches
Added includes in models to prevent n+1 queries
N+1 queries found with the help of bullet gem
Showing
9 changed files
with
14 additions
and
14 deletions
Show diff stats
app/controllers/my_profile/cms_controller.rb
... | ... | @@ -59,7 +59,7 @@ class CmsController < MyProfileController |
59 | 59 | conditions = ['type != ?', 'RssFeed'] |
60 | 60 | end |
61 | 61 | |
62 | - @articles = @article.children.reorder("case when type = 'Folder' then 0 when type ='Blog' then 1 else 2 end, updated_at DESC, name").paginate( | |
62 | + @articles = @article.children.includes(:parent).reorder("case when type = 'Folder' then 0 when type ='Blog' then 1 else 2 end, updated_at DESC, name").paginate( | |
63 | 63 | :conditions => conditions, |
64 | 64 | :per_page => per_page, |
65 | 65 | :page => params[:npage] | ... | ... |
app/controllers/public/content_viewer_controller.rb
... | ... | @@ -221,7 +221,7 @@ class ContentViewerController < ApplicationController |
221 | 221 | # relation. |
222 | 222 | posts = posts.native_translations if blog_with_translation?(@page) |
223 | 223 | |
224 | - @posts = posts.paginate({ :page => params[:npage], :per_page => @page.posts_per_page }.merge(Article.display_filter(user, profile))).to_a | |
224 | + @posts = posts.paginate({ :page => params[:npage], :per_page => @page.posts_per_page }.merge(Article.display_filter(user, profile))).includes(:author).to_a | |
225 | 225 | |
226 | 226 | if blog_with_translation?(@page) |
227 | 227 | @posts.replace @posts.map{ |p| p.get_translation_to(FastGettext.locale) }.compact | ... | ... |
app/models/article.rb
... | ... | @@ -58,10 +58,10 @@ class Article < ActiveRecord::Base |
58 | 58 | belongs_to :last_changed_by, :class_name => 'Person', :foreign_key => 'last_changed_by_id' |
59 | 59 | belongs_to :created_by, :class_name => 'Person', :foreign_key => 'created_by_id' |
60 | 60 | |
61 | - has_many :comments, :class_name => 'Comment', :foreign_key => 'source_id', :dependent => :destroy, :order => 'created_at asc' | |
61 | + has_many :comments, :class_name => 'Comment', :foreign_key => 'source_id', :dependent => :destroy, :order => 'comments.created_at asc', :include => [:author, :children] | |
62 | 62 | |
63 | 63 | has_many :article_categorizations, :conditions => [ 'articles_categories.virtual = ?', false ] |
64 | - has_many :categories, :through => :article_categorizations | |
64 | + has_many :categories, :through => :article_categorizations, :include => [:environment] | |
65 | 65 | |
66 | 66 | has_many :article_categorizations_including_virtual, :class_name => 'ArticleCategorization' |
67 | 67 | has_many :categories_including_virtual, :through => :article_categorizations_including_virtual, :source => :category |
... | ... | @@ -236,7 +236,7 @@ class Article < ActiveRecord::Base |
236 | 236 | # retrieves all articles belonging to the given +profile+ that are not |
237 | 237 | # sub-articles of any other article. |
238 | 238 | scope :top_level_for, lambda { |profile| |
239 | - {:conditions => [ 'parent_id is null and profile_id = ?', profile.id ]} | |
239 | + {:conditions => [ 'parent_id is null and profile_id = ?', profile.id ], :include => [:profile]} | |
240 | 240 | } |
241 | 241 | |
242 | 242 | scope :public, |
... | ... | @@ -460,7 +460,7 @@ class Article < ActiveRecord::Base |
460 | 460 | end |
461 | 461 | |
462 | 462 | scope :published, :conditions => ['articles.published = ?', true] |
463 | - scope :folders, lambda {|profile|{:conditions => ['articles.type IN (?)', profile.folder_types] }} | |
463 | + scope :folders, lambda {|profile|{:conditions => ['articles.type IN (?)', profile.folder_types], :include => [:parent] }} | |
464 | 464 | scope :no_folders, lambda {|profile|{:conditions => ['articles.type NOT IN (?)', profile.folder_types]}} |
465 | 465 | scope :galleries, :conditions => [ "articles.type IN ('Gallery')" ] |
466 | 466 | scope :images, :conditions => { :is_image => true } |
... | ... | @@ -469,7 +469,7 @@ class Article < ActiveRecord::Base |
469 | 469 | |
470 | 470 | scope :more_popular, :order => 'hits DESC' |
471 | 471 | scope :more_comments, :order => "comments_count DESC" |
472 | - scope :more_recent, :order => "created_at DESC" | |
472 | + scope :more_recent, :order => "articles.created_at DESC" | |
473 | 473 | |
474 | 474 | def self.display_filter(user, profile) |
475 | 475 | return {:conditions => ['articles.published = ?', true]} if !user | ... | ... |
app/models/category.rb
... | ... | @@ -16,7 +16,7 @@ class Category < ActiveRecord::Base |
16 | 16 | |
17 | 17 | # Finds all top level categories for a given environment. |
18 | 18 | scope :top_level_for, lambda { |environment| |
19 | - {:conditions => ['parent_id is null and environment_id = ?', environment.id ]} | |
19 | + {:conditions => ['parent_id is null and environment_id = ?', environment.id ], :include => [:children]} | |
20 | 20 | } |
21 | 21 | |
22 | 22 | scope :on_level, lambda { |parent| {:conditions => {:parent_id => parent}} } | ... | ... |
app/models/comment.rb
... | ... | @@ -18,7 +18,7 @@ class Comment < ActiveRecord::Base |
18 | 18 | has_many :children, :class_name => 'Comment', :foreign_key => 'reply_of_id', :dependent => :destroy |
19 | 19 | belongs_to :reply_of, :class_name => 'Comment', :foreign_key => 'reply_of_id' |
20 | 20 | |
21 | - scope :without_reply, :conditions => ['reply_of_id IS NULL'] | |
21 | + scope :without_reply, :conditions => ['comments.reply_of_id IS NULL'] | |
22 | 22 | |
23 | 23 | # unauthenticated authors: |
24 | 24 | validates_presence_of :name, :if => (lambda { |record| !record.email.blank? }) | ... | ... |
app/models/person.rb
... | ... | @@ -434,7 +434,7 @@ class Person < Profile |
434 | 434 | end |
435 | 435 | |
436 | 436 | def already_reported?(profile) |
437 | - abuse_reports.any? { |report| report.abuse_complaint.reported == profile && report.abuse_complaint.opened? } | |
437 | + abuse_reports.includes({:abuse_complaint => :reported}).any? { |report| report.abuse_complaint.reported == profile && report.abuse_complaint.opened? } | |
438 | 438 | end |
439 | 439 | |
440 | 440 | def register_report(abuse_report, profile) | ... | ... |
app/models/profile.rb
... | ... | @@ -190,7 +190,7 @@ class Profile < ActiveRecord::Base |
190 | 190 | belongs_to :preferred_domain, :class_name => 'Domain', :foreign_key => 'preferred_domain_id' |
191 | 191 | belongs_to :environment |
192 | 192 | |
193 | - has_many :articles, :dependent => :destroy | |
193 | + has_many :articles, :dependent => :destroy, :include => [:profile] | |
194 | 194 | belongs_to :home_page, :class_name => Article.name, :foreign_key => 'home_page_id' |
195 | 195 | |
196 | 196 | has_many :files, :class_name => 'UploadedFile' | ... | ... |
lib/acts_as_having_posts.rb
... | ... | @@ -2,7 +2,7 @@ module ActsAsHavingPosts |
2 | 2 | |
3 | 3 | module ClassMethods |
4 | 4 | def acts_as_having_posts(options = {}) |
5 | - has_many :posts, { :class_name => 'Article', :foreign_key => 'parent_id', :source => :children, :conditions => [ 'articles.type != ?', 'RssFeed' ], :order => 'published_at DESC, id DESC' }.merge(options) | |
5 | + has_many :posts, { :class_name => 'Article', :foreign_key => 'parent_id', :source => :children, :conditions => [ 'articles.type != ?', 'RssFeed' ], :order => 'published_at DESC, id DESC', :include => [:parent, :profile] }.merge(options) | |
6 | 6 | |
7 | 7 | attr_accessor :feed_attrs |
8 | 8 | ... | ... |
lib/spammable.rb
... | ... | @@ -12,8 +12,8 @@ module Spammable |
12 | 12 | def self.extended (base) |
13 | 13 | if base.respond_to?(:scope) |
14 | 14 | base.class_eval do |
15 | - scope :without_spam, :conditions => ['spam IS NULL OR spam = ?', false] | |
16 | - scope :spam, :conditions => ['spam = ?', true] | |
15 | + scope :without_spam, :conditions => ["#{base.table_name}.spam IS NULL OR #{base.table_name}.spam = ?", false] | |
16 | + scope :spam, :conditions => ["#{base.table_name}.spam = ?", true] | |
17 | 17 | end |
18 | 18 | end |
19 | 19 | end | ... | ... |