Skip to content

Commit

Permalink
Merge pull request #51 from hyoshida/remove-performance-bottleneck-on…
Browse files Browse the repository at this point in the history
…-search-products

Remove a performance bottleneck on the search products
  • Loading branch information
YOSHIDA Cake committed Nov 20, 2015
2 parents 2daa5a0 + af0a32e commit 62e576e
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 9 deletions.
8 changes: 1 addition & 7 deletions core/app/models/comable/product.rb
Expand Up @@ -7,7 +7,7 @@ class Product < ActiveRecord::Base
include Comable::Product::Csvable

has_many :variants, class_name: Comable::Variant.name, inverse_of: :product, dependent: :destroy
has_many :images, class_name: Comable::Image.name, dependent: :destroy
has_many :images, -> { order(:id) }, class_name: Comable::Image.name, dependent: :destroy
has_and_belongs_to_many :categories, class_name: Comable::Category.name, join_table: :comable_products_categories

accepts_nested_attributes_for :variants, allow_destroy: true
Expand All @@ -24,12 +24,6 @@ class Product < ActiveRecord::Base

PREVIEW_SESSION_KEY = :preview_product

# Add conditions for the images association.
# Override method of the images association to support Rails 3.x.
def images
super.order(:id)
end

def image_url
image = images.first
return image.url if image
Expand Down
2 changes: 1 addition & 1 deletion frontend/app/controllers/comable/home_controller.rb
Expand Up @@ -8,7 +8,7 @@ def show
private

def load_products
@products = Comable::Product.limit(5)
@products = Comable::Product.includes(:images, :variants).limit(4)
end
end
end
2 changes: 1 addition & 1 deletion frontend/app/controllers/comable/products_controller.rb
Expand Up @@ -3,7 +3,7 @@ class ProductsController < Comable::ApplicationController
before_filter :load_category_and_products, only: :index

def index
@products = @products.page(params[:page]).per(Comable::Config.products_per_page)
@products = @products.includes(:images, :variants).page(params[:page]).per(Comable::Config.products_per_page)
end

def show
Expand Down

0 comments on commit 62e576e

Please sign in to comment.