remove N+1 queries in contributions#index
This commit is contained in:
parent
a477bdc447
commit
8bb553701a
2 changed files with 15 additions and 11 deletions
|
@ -7,6 +7,13 @@ class ContributionsController < ApplicationController
|
||||||
@contributions = Contribution.includes(:user)
|
@contributions = Contribution.includes(:user)
|
||||||
end
|
end
|
||||||
@contributions = @contributions.recent.paginate :page => params[:page]
|
@contributions = @contributions.recent.paginate :page => params[:page]
|
||||||
Contribution.preload_contributeds_and_parents @contributions
|
Contribution.preload_contributeds_and_parents(
|
||||||
|
@contributions,
|
||||||
|
:scopes => {
|
||||||
|
'Item' => Item.includes(:translations),
|
||||||
|
'PetType' => PetType.includes({:species => :translations,
|
||||||
|
:color => :translations})
|
||||||
|
}
|
||||||
|
)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -26,12 +26,9 @@ class Contribution < ActiveRecord::Base
|
||||||
}
|
}
|
||||||
CONTRIBUTED_CHILDREN = CONTRIBUTED_RELATIONSHIPS.keys
|
CONTRIBUTED_CHILDREN = CONTRIBUTED_RELATIONSHIPS.keys
|
||||||
CONTRIBUTED_TYPES = CONTRIBUTED_CHILDREN + CONTRIBUTED_RELATIONSHIPS.values
|
CONTRIBUTED_TYPES = CONTRIBUTED_CHILDREN + CONTRIBUTED_RELATIONSHIPS.values
|
||||||
CONTRIBUTED_BASES = {}
|
def self.preload_contributeds_and_parents(contributions, options={})
|
||||||
CONTRIBUTED_TYPES.each do |type|
|
options[:scopes] ||= {}
|
||||||
base = type == 'SwfAsset' ? SwfAsset.object_assets : type.constantize
|
|
||||||
CONTRIBUTED_BASES[type] = base
|
|
||||||
end
|
|
||||||
def self.preload_contributeds_and_parents(contributions)
|
|
||||||
# Initialize the groups we'll be using for quick access
|
# Initialize the groups we'll be using for quick access
|
||||||
contributions_by_type = {}
|
contributions_by_type = {}
|
||||||
contributed_by_type = {}
|
contributed_by_type = {}
|
||||||
|
@ -56,8 +53,8 @@ class Contribution < ActiveRecord::Base
|
||||||
# Load contributed objects without parents, prepare them for easy access
|
# Load contributed objects without parents, prepare them for easy access
|
||||||
# for future assignment to contributions and looking up parents
|
# for future assignment to contributions and looking up parents
|
||||||
CONTRIBUTED_CHILDREN.each do |type|
|
CONTRIBUTED_CHILDREN.each do |type|
|
||||||
base = CONTRIBUTED_BASES[type]
|
scope = options[:scopes][type] || type.constantize.scoped
|
||||||
base.find(needed_ids_by_type[type]).each do |contributed|
|
scope.find(needed_ids_by_type[type]).each do |contributed|
|
||||||
contributed_by_type[type] << contributed
|
contributed_by_type[type] << contributed
|
||||||
contributed_by_type_and_id[type][contributed.id] = contributed
|
contributed_by_type_and_id[type][contributed.id] = contributed
|
||||||
end
|
end
|
||||||
|
@ -67,10 +64,10 @@ class Contribution < ActiveRecord::Base
|
||||||
# contributed objects of that class. all_by_ids_or_children properly
|
# contributed objects of that class. all_by_ids_or_children properly
|
||||||
# assigns parents to children, as well
|
# assigns parents to children, as well
|
||||||
CONTRIBUTED_RELATIONSHIPS.each do |child_type, type|
|
CONTRIBUTED_RELATIONSHIPS.each do |child_type, type|
|
||||||
base = CONTRIBUTED_BASES[type]
|
scope = options[:scopes][type] || type.constantize.scoped
|
||||||
ids = needed_ids_by_type[type]
|
ids = needed_ids_by_type[type]
|
||||||
children = contributed_by_type[child_type]
|
children = contributed_by_type[child_type]
|
||||||
base.all_by_ids_or_children(ids, children).each do |contributed|
|
scope.all_by_ids_or_children(ids, children).each do |contributed|
|
||||||
contributed_by_type_and_id[type][contributed.id] = contributed
|
contributed_by_type_and_id[type][contributed.id] = contributed
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue