Commit b5aeeccd by Hoang Phuc Do

N + 1 query fix

parent 03678484
...@@ -93,6 +93,8 @@ group :development do ...@@ -93,6 +93,8 @@ group :development do
gem 'spring-watcher-listen', '~> 2.0.0' gem 'spring-watcher-listen', '~> 2.0.0'
# When mail is sent from your application, Letter Opener will open a preview in the browser instead of sending. # When mail is sent from your application, Letter Opener will open a preview in the browser instead of sending.
gem 'letter_opener', '~> 1.4', '>= 1.4.1' gem 'letter_opener', '~> 1.4', '>= 1.4.1'
# help to kill N+1 queries and unused eager loading.
gem 'bullet', '~> 5.5', '>= 5.5.1'
end end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem # Windows does not include zoneinfo files, so bundle the tzinfo-data gem
......
...@@ -54,6 +54,9 @@ GEM ...@@ -54,6 +54,9 @@ GEM
autoprefixer-rails (>= 5.2.1) autoprefixer-rails (>= 5.2.1)
sass (>= 3.3.4) sass (>= 3.3.4)
builder (3.2.3) builder (3.2.3)
bullet (5.5.1)
activesupport (>= 3.0.0)
uniform_notifier (~> 1.10.0)
byebug (9.0.6) byebug (9.0.6)
capybara (2.14.0) capybara (2.14.0)
addressable addressable
...@@ -287,6 +290,7 @@ GEM ...@@ -287,6 +290,7 @@ GEM
uglifier (3.2.0) uglifier (3.2.0)
execjs (>= 0.3.0, < 3) execjs (>= 0.3.0, < 3)
unicode-display_width (1.2.1) unicode-display_width (1.2.1)
uniform_notifier (1.10.0)
warden (1.2.7) warden (1.2.7)
rack (>= 1.0) rack (>= 1.0)
web-console (3.5.1) web-console (3.5.1)
...@@ -307,6 +311,7 @@ PLATFORMS ...@@ -307,6 +311,7 @@ PLATFORMS
DEPENDENCIES DEPENDENCIES
active_link_to (~> 1.0, >= 1.0.4) active_link_to (~> 1.0, >= 1.0.4)
bootstrap-sass (~> 3.3.6) bootstrap-sass (~> 3.3.6)
bullet (~> 5.5, >= 5.5.1)
byebug byebug
capybara (~> 2.13) capybara (~> 2.13)
carrierwave (~> 1.0) carrierwave (~> 1.0)
......
...@@ -8,7 +8,7 @@ class AdminController < ApplicationController ...@@ -8,7 +8,7 @@ class AdminController < ApplicationController
# GET /admin/orders # GET /admin/orders
def orders def orders
@orders = Order.page(params[:page]).per(5) @orders = Order.includes(:product_items).page(params[:page]).per(5)
end end
private private
......
class CategoriesController < ApplicationController class CategoriesController < ApplicationController
def show def show
@category = Category.find(params[:id]) @category = Category.find(params[:id])
@products = @category.products.page(params[:page]).per(5) @products = @category.products.includes(:user).page(params[:page]).per(5)
end end
end end
\ No newline at end of file
class StaticPagesController < ApplicationController class StaticPagesController < ApplicationController
def index def index
@latest_products = Product.page(params[:page]).per(5) @latest_products = Product.includes(:user).page(params[:page]).per(5)
# @TODO: Get recommended products # @TODO: Get recommended products
@recommended_products = Product.last(6) @recommended_products = Product.last(6)
# session.clear # session.clear
......
...@@ -13,6 +13,6 @@ class UsersController < ApplicationController ...@@ -13,6 +13,6 @@ class UsersController < ApplicationController
# GET /users/orders # GET /users/orders
def orders def orders
@orders = current_user.orders.page(params[:page]).per(5) @orders = current_user.orders.includes(:product_items).page(params[:page]).per(5)
end end
end end
\ No newline at end of file
...@@ -10,7 +10,7 @@ class Product < ApplicationRecord ...@@ -10,7 +10,7 @@ class Product < ApplicationRecord
validates :user_id, presence: true validates :user_id, presence: true
def in_stock?(required_quantity) def in_stock?(required_quantity)
return false if required_quantity <= 0 return false if required_quantity.to_i < 0
quantity >= required_quantity.to_i quantity >= required_quantity.to_i
end end
......
...@@ -19,6 +19,6 @@ class Search ...@@ -19,6 +19,6 @@ class Search
def matched_products(products) def matched_products(products)
product_ids = products.map { |product| product[:id.to_s] } product_ids = products.map { |product| product[:id.to_s] }
Product.find(product_ids) Product.includes(:user).find(product_ids)
end end
end end
\ No newline at end of file
...@@ -53,4 +53,13 @@ Rails.application.configure do ...@@ -53,4 +53,13 @@ Rails.application.configure do
config.file_watcher = ActiveSupport::EventedFileUpdateChecker config.file_watcher = ActiveSupport::EventedFileUpdateChecker
config.action_mailer.delivery_method = :letter_opener config.action_mailer.delivery_method = :letter_opener
config.after_initialize do
Bullet.enable = true
Bullet.alert = true
Bullet.bullet_logger = true
Bullet.console = true
Bullet.rails_logger = true
Bullet.add_footer = true
end
end end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment