adds eager loading to avoid N+1 queries

This commit is contained in:
Juanjo Bazán
2015-08-18 16:54:49 +02:00
parent b84fb65b93
commit ced2a05063
4 changed files with 4 additions and 2 deletions

View File

@@ -5,7 +5,7 @@ class DebatesController < ApplicationController
respond_to :html, :js
def index
@debates = Debate.search(params)
@debates = Debate.includes(:tags).search(params)
set_debate_votes(@debates)
end

View File

@@ -2,7 +2,7 @@ class WelcomeController < ApplicationController
skip_authorization_check
def index
@featured_debates = Debate.limit(9)
@featured_debates = Debate.includes(:tags).limit(9)
set_debate_votes(@featured_debates)
end

View File

@@ -11,6 +11,7 @@ class Comment < ActiveRecord::Base
belongs_to :commentable, polymorphic: true
belongs_to :user
default_scope { includes(:user) }
scope :recent, -> { order(id: :desc) }
def self.build(commentable, user, body)

View File

@@ -18,6 +18,7 @@ class User < ActiveRecord::Base
accepts_nested_attributes_for :organization
default_scope { includes(:organization) }
scope :administrators, -> { joins(:administrators) }
scope :moderators, -> { joins(:moderator) }
scope :organizations, -> { joins(:organization) }