From 2ca891c9f9be8c840a7b31015eefc56c31a363bc Mon Sep 17 00:00:00 2001 From: YOSHIDA Hiroki Date: Wed, 18 Nov 2015 16:41:07 +0900 Subject: [PATCH 1/3] Includes images and variants of products --- frontend/app/controllers/comable/products_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frontend/app/controllers/comable/products_controller.rb b/frontend/app/controllers/comable/products_controller.rb index 5527701d..fa18b8ed 100644 --- a/frontend/app/controllers/comable/products_controller.rb +++ b/frontend/app/controllers/comable/products_controller.rb @@ -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 From dbec2feb71d381f343b7672d294e7e0d6a6f1e4b Mon Sep 17 00:00:00 2001 From: YOSHIDA Hiroki Date: Wed, 18 Nov 2015 16:44:28 +0900 Subject: [PATCH 2/3] Change the method of the sort condition for images scope Fix the problem that 'Comable::Image Load' runs twice like below: ``` irb> Comable::Product.includes(:images).first.image_url Comable::Product Load (0.7ms) SELECT "comable_products".* FROM "comable_products" ORDER BY "comable_products"."id" ASC LIMIT 1 Comable::Image Load (0.4ms) SELECT "comable_images".* FROM "comable_images" WHERE "comable_images"."product_id" IN (1) Comable::Image Load (0.5ms) SELECT "comable_images".* FROM "comable_images" WHERE "comable_images"."product_id" = $1 ORDER BY "comable_images"."id" ASC LIMIT 1 [["product_id", 1]] ``` --- core/app/models/comable/product.rb | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/core/app/models/comable/product.rb b/core/app/models/comable/product.rb index f1274351..6d9d3009 100644 --- a/core/app/models/comable/product.rb +++ b/core/app/models/comable/product.rb @@ -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 @@ -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 From af0a32e88259c71b3ee2d423fb39f0ec9b06913d Mon Sep 17 00:00:00 2001 From: YOSHIDA Hiroki Date: Fri, 20 Nov 2015 13:09:09 +0900 Subject: [PATCH 3/3] Includes images and variants of products in home --- frontend/app/controllers/comable/home_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frontend/app/controllers/comable/home_controller.rb b/frontend/app/controllers/comable/home_controller.rb index d7079ccf..234cf3be 100644 --- a/frontend/app/controllers/comable/home_controller.rb +++ b/frontend/app/controllers/comable/home_controller.rb @@ -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